Home > Software engineering >  How to assign value of one variable to name of another? [JavaScript, Vue.js]
How to assign value of one variable to name of another? [JavaScript, Vue.js]

Time:11-28

I have 2 JSON arrays

"feeds":[
  {
     "created_at":"2021-11-28T00:11:38Z",
     "entry_id":17901,
     "field1":"16.40000",
     "field2":"45.50000",
     "field3":"16.06250",
     "field4":"5.20000",
     "field5":"49.79300",
     "field7":"1",
     "field8":"390267909"
  }
]

and

 "measurements":[
   {
      "user_id":1,
      "measurements_id":1,
      "fieldId":1,
      "name":"Unutrašnja temperatura vazduha",
      "unit":"°C",
      "url":"inner-temp.png"
   }]

So in my code when I want to access value of field1 I do myField=feeds.field1 and then value of myField is 16.4000. But now I need to add those values dynamically, depending on the fieldId property value from second JSON array measurements.

Basically, I need something like: myField='feeds.field' measurements.fieldId and then again myField should be 16.4000.

CodePudding user response:

You can use bracket notation to access object members like so:

myField = feeds['field'   measurements.fieldId];

You can read more about it on MDN

  • Related