开发者

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);
  }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜