How to work with objects in JavaScript?
How to work with object, if I want...
var object = { 'title': value };
alert( object[ /* Whatever */ ] ); // Should return 'title' NOT 开发者_StackOverflowvalue
Thanks.
Use a for...in
loop to enumerate an object's keys, like this:
for(var key in object) {
alert(key); //to get the key's value, use object[key]
}
To be safe, in case someone messed with the object prototype, use .hasOwnProperty()
like this:
for(var key in object) {
if(object.hasOwnProperty(key)) {
alert(key);
}
}
精彩评论