Saturday, 2 September 2017

How to access a javascript object value without knowing the key











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?


Answer



data = {
a : 2,
b : 3

}

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

No comments:

Post a Comment

casting - Why wasn't Tobey Maguire in The Amazing Spider-Man? - Movies & TV

In the Spider-Man franchise, Tobey Maguire is an outstanding performer as a Spider-Man and also reprised his role in the sequels Spider-Man...