-1

I have an array that I want to loop with a for.

for(var j = 0; j < outPutData.length; j++)
{
    if(outPutData[j].pregunta1==1) {  }
}

pregunta1, pregunta2, etc. are some of the variables.

If I print the value of outPutData[j].pregunta1 with an alert() it shows the correct value, but when I try:

for(var j = 0; j < outPutData.length; j++)
{
    if(outPutData[j].pregunta+j==1) {  }

OR

    if(outPutData[j].pregunta+""+j==2) {  }
}

an error is shown. Why?

What I expect to have is

outPutData[j].pregunta1
outPutData[j].pregunta2
outPutData[j].pregunta3
... (more till 200)...
outPutData[j].pregunta200

What am I doing wrong?

Thanks!

Jordi 45454
  • 275
  • 2
  • 3
  • 11

1 Answers1

0

Try this:

outPutData[j]['pregunta' + j]

you can access properties of object by string using []:

var obj = {prop: "value"}

var a = obj.prop;
// is the same as
var b = obj['prop'];
madox2
  • 49,493
  • 17
  • 99
  • 99