Iterarting over array of objects and get unique values of each object

Tags: , ,



I have this variable inside data():

jsonStatham: {
  "uniqueOne": {
    "field1": "",
    "field2": "",
    "field3": "",
    "field4": "",
    "field5": "",
    "freeTextArea": ""
  },
  "uniqueTwo": {
    "field1": "",
    "field2": "",
    "field3":"",
    "field4":"",
    "field5":"",
    "freeTextArea":""
  },
  "uniqueThree": {
    "field1": "",
    "field2": "",
    "freeTextArea": ""
  }
},

What I want is to check if a value from this input field:

<input type="text" name="platform" placeholder="Platform" id="platform" v-model="platform" required/>

is matching one of the keys of “jsonStatham” (uniqueOne/Two/Three) and then push the keys of the matching key into an array. so if the input === uniqueOne, so this array:

inputFields: [

],

Will look like this: inputFields[“field1″,”field2″,”field3″,”field4″,”field5”]

That’s what I tried:

appendFields() {

      for (const [key, value] of Object.entries(this.jsonStatham)) {
        if(this.brand === this.jsonStatham[key]){
          //console.log("Brand =>", this.brand)
        }
        //console.log(`${key}: ${value}`);
        this.inputFields.push({
          [key]:value
        })
      }
      //console.log("ALL input Fields: n",this.inputFields)
    },

What I get in inputFields is “uniqueOne”,”uniqueTwo”,”uniqueThree”

Answer

if (this.jsonStatham.hasOwnProperty(this.brand)) {
  this.inputFields.push(...Object.keys(this.jsonStatham[this.brand]));
}


Source: stackoverflow