How would I get the key name for the follow? E.g I want “button1” and “button2”?
var buttons = {
button1: {
text: 'Close',
onclick: function(){
}
},
button2: {
text: 'Close2',
onclick: function(){
}
}
}
var i;
for(i in buttons){
if(buttons.hasOwnProperty(i)){
alert(buttons[i].text);
}
}
I tried using .push() although this didn’t work.
This might be better understood if you modified the wording up a bit:
Note here that you’re iterating over the properties of the object, using
propertyas a reference to each during each subsequent cycle.MSDN says of
for ( variable in [object | array ] )the following:Note also that the property order of an object is not constant, and can change, unlike the index order of an array. That might come in handy.