Friday, May 17, 2024
 Popular · Latest · Hot · Upcoming
32
rated 0 times [  36] [ 4]  / answers: 1 / hits: 41618  / 12 Years ago, mon, september 10, 2012, 12:00:00

Possible Duplicate:

How do I enumerate the properties of a javascript object?






If I have a javascript object like this :



data = {
a : 2,
b : 3
}


but a and b are arbitrary and decided at runtime. Is there any way to go through the object
and access all properties without knowing the key?


More From » javascript

 Answers
31
data = {
a : 2,
b : 3
}

for(var propName in data) {
if(data.hasOwnProperty(propName)) {
var propValue = data[propName];
// do something with each element here
}
}

[#83164] Friday, September 7, 2012, 12 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
beatrizrheaq

Total Points: 73
Total Questions: 89
Total Answers: 107

Location: Jersey
Member since Fri, Oct 1, 2021
3 Years ago
;