14

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

1 Answer 1

27
var names = [];
for(var key in x) {
   if(x.hasOwnProperty(key)) {
      names.push(key);
   }
}
alert(names.join(', ')); //a, b
Sign up to request clarification or add additional context in comments.

1 Comment

Guys, sorry about the undefined k typo. I meant key. :)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.