开发者

JavaScript: Retrieve key names from an object?

Say I have this:

var x = {  
          a:{a1:"z", a2:"x"},
          b:{b1:"y", b2:"w"}
}

Is there a way to iterate over x to get "a" and "b"?

I want the member name, not its content (I don'开发者_运维百科t want to get {a1:"z", a2:"x"}).

Thanks


var names = [];
for(var key in x) {
   if(x.hasOwnProperty(key)) {
      names.push(key);
   }
}
alert(names.join(', ')); //a, b
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜