Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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

>Solution :

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

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

You can read more about it on MDN

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading