celsowm
celsowm

Reputation: 404

How access a attribute from a Object dynamically in JavaScript?

In my object

obj.resposta1
obj.resposta2
obj.resposta3
obj.resposta4

how access the values of each inside a

for ( var int = 1; int < 5; int++)

?

Thanks, Celso

Upvotes: 1

Views: 119

Answers (2)

Shef
Shef

Reputation: 45589

Try this:

for ( var int = 1; int < 5; int++){
    obj['resposta'+int];
}

Upvotes: 1

C. K. Young
C. K. Young

Reputation: 223023

var i;
for (i = 1; i < 5; ++i) {
    alert(obj['resposta' + i]);
}

Upvotes: 6

Related Questions