-3

There is any way to access using index + string to this array?, knowing that there is n options, using for doing something like this?

var myArray = {input-name: 'xxx', option1: 20, option2: 50, option3: 40...option10:100};
for (var i=1;i<=10;i++)
{
  console.log(myArray.option+i);
 }
Z xie
  • 9
  • 2

1 Answers1

0

Yup, just index the array with the key like this:

var myArray = {
    inputName: "xxx",
    option1: 20,
    option2: 50,
    option3: 40,
    option4: 30,
    option5: 100
}
    
for(var i = 1; i <= 5; i++) {
    console.log(myArray["option" + i])
}
zuodan
  • 16
  • 1
  • 2