As of JavaScript 1.8.5 you can use Object.keys(obj)
to get an Array of properties defined on the object itself (the ones that return true for obj.hasOwnProperty(key)
).
Object.keys(obj).forEach(function(key,index) {
// key: the name of the object key
// index: the ordinal position of the key within the object
});
This is better (and more readable) than using a for-in loop.
Its supported on these browsers:
- Firefox (Gecko): 4 (2.0)
- Chrome: 5
- Internet Explorer: 9
See the Mozilla Developer Network Object.keys()'s reference for futher information.