Return to Snippet

Revision: 40883
at February 9, 2011 10:29 by iroybot


Initial Code
var obj = JSON.parse('{"someKey":10,"otherKey":20}') ;

for(var prop in obj) {
    if(obj.hasOwnProperty(prop))
        console.info(obj[prop]); // value
        console.info(prop); // key name
}

Initial URL
http://stackoverflow.com/questions/587881/how-to-iterate-over-every-property-of-an-object-in-javascript/588276#588276

Initial Description
I wanted to pass a form submission (array) as a JSON object to another function.
Some form field were created with JS, to identify them their names have been concatenated with unique IDs, etc.
In order to access values of that JSON obj I needed to find out the key names in order to get data out of it.

Initial Title
getting an objects key values

Initial Tags
js, object, array, json

Initial Language
JavaScript