user8395584
user8395584

Reputation:

Push unique key and value into array in Vue

I'm trying to push key + value into the array with their unique values.

Trying like this:

for (const [key, value] of Object.entries(check)) {
          console.log(`${key}: ${value}`);
          this.inputFields.push({
              //`${key}' : '${value}`
          })
        }

Console.log output give me the correct values:

Username: ''
Password: ''

What i want to do is to push into inputFields the key and value so input fields will be:

Username:
Password:

Without the need of hardcode it into it, because every object in my array have different key and values.

Upvotes: 0

Views: 1192

Answers (2)

BNilsou
BNilsou

Reputation: 905

Just a simple : this.inputFields[key] = value; should work fine.

Upvotes: 2

Boussadjra Brahim
Boussadjra Brahim

Reputation: 1

You could use [] to wrap the computed key :

for (const [key, value] of Object.entries(check)) {
          console.log(`${key}: ${value}`);
          this.inputFields.push({
              [key]:value
          })
        }

Upvotes: 2

Related Questions