-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!

1
  • You should probably just be making pregunta an array though. Commented Jan 26, 2016 at 17:19

1 Answer 1

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'];
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, it solved the problem!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.