I want to loop through this object and add the 'loc' value to an array if their side = 2. What am I doing wrong?
2025 is the the room object and the entire things is rooms.
//Object
{
"2025": {
"tom": {
"side": 1,
"loc": 111
},
"billy": {
"side": 2,
"loc": 222
},
"joe": {
"side": 2,
"loc": 333
},
"bob": {
"side": 1,
"loc": 444
}
}
}
//Code
var side2 = [];
for (var key in rooms[room]) {
if (rooms[room].hasOwnProperty(key)) {
var obj = rooms[room][key];
for (var prop in obj) {
if (obj.hasOwnProperty(prop)) {
if(prop == 'loc') {
if(obj[prop] == 2) {
side2.push(key);
}
}
}
}
}
}
console.log(side2);
locproperty has the vaue2? Stepping through this in the debugger ought to help you to figure it out. But why are you looping through the keys for findlocanyway? You can just doobj.loc.locbut forside. You want to do something likeif(prop == 'side') { if(obj[prop] == 2) { side2.push(key); }