Im trying to improve my javascript an running into somewhat of a dead end here.
var schemes = {
"own" : {
"creatures" : ["creature1","creature2","creature3","creature4"],
"spells" : ["spell1","spell2","spell3","spell4"],
"items" : ["item1","item2","item3","item4"]
},
"enemy" : {
"creatures" : ["creature1","creature2","creature3","hidden"],
"spells" : ["spell1","spell2","hidden","hidden"],
"items" : ["item1","item2","item3","hidden"]
}
};
This is my array.
Im then trying to do a for each (as I know from php), but it seems javascript thinks schemes is an object and thus unable to do a:
for (var i=0;i<schemes.length;i++) {
//code
}
What am I missing? schemes.length says undefined
schemesis indeed an “object”, and as such has no.length.You can use
Object.keys(schemes)to obtain an array of the keys, or: