JS: List all properties and values of an object

 If you need to generate forms dynamically to update some JS objects for instance:

var obj = {prop1: 5, prop2: 13, prop3: 8};
var keys = Object.keys(obj);

var key;
var value;
for (var i=0; i<keys.length; i++){
    key = keys[i];
    value = obj[key];

    console.log(key);
    console.log(value);
}

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s