getting an objects key values


/ Published in: JavaScript
Save to your folder(s)

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.


Copy this code and paste it in your HTML
  1. var obj = JSON.parse('{"someKey":10,"otherKey":20}') ;
  2.  
  3. for(var prop in obj) {
  4. if(obj.hasOwnProperty(prop))
  5. console.info(obj[prop]); // value
  6. console.info(prop); // key name
  7. }

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

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.