Output the name and value of the properties in a javascript object


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

The code below will loop through the properties in an object and alert a string. myObject is in JSON notation. The "for ( x in y)" construct does not work if the Prototype.js library is included on the page


Copy this code and paste it in your HTML
  1. // output the name and value of all the objects properties
  2. var s = "";
  3.  
  4. for (var property in myObject)
  5. {
  6. s = s + "\n "+property +": " + myObject[property] ;
  7. }
  8.  
  9. alert(s);

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.