-2

Is it possible to put the counter variable value inside a variable?

I just want to have in the first loop testData_0_name, in the second testData_1_name etc. Those are my parameters in my records

for(var i = 0; i < record.length;i++)
{
    record[i].testData_ **here I want to have  "i"**_name ....
}

I don't really want to use any global scopes or something with [window].

mplungjan
  • 169,008
  • 28
  • 173
  • 236
randomcat
  • 17
  • 6

1 Answers1

1

var records = [{'testData_0_name': 0}, {'testData_1_name': 1}, {'testData_2_name': 2}, {'testData_3_name': 3}];

for(var i = 0; i < records.length;i++)
{
    console.log(records[i]['testData_'+i+'_name'])
}

You can use bracket notation to access object properties and we can also customize the string in bracket notation like other strings.

Rohit Agrawal
  • 1,496
  • 9
  • 20