Increment a "Property path" in an ajax

Asked

Viewed 13 times

-1

I need to increment a property path in a ajax, I need to add numbers in front of the property in a for and receive its value, follow example:

data.data[0].genero1

I need to get the genero2 now, I tried that but it didn’t work:

for (var i = 1; i <= 5; i++) {
    var p = "genero"+i;
    console.log(p);
    console.log(data.data[0].p);
}

That one for above does not give error, but returns undefined

To var p returns the string correctly, the second console.log that gives undefined

  • Because the "-1" ?

1 answer

1


I managed to solve, follows example:

for (var i = 1; i <= 5; i++) {
    var p = "genero"+i;
    console.log(p);
    console.log(data.data[0][p]);
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.