0

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);
2
  • 1
    Because no loc property has the vaue 2? Stepping through this in the debugger ought to help you to figure it out. But why are you looping through the keys for find loc anyway? You can just do obj.loc. Commented Nov 14, 2015 at 3:02
  • Yeah there is no value 2 for loc but for side. You want to do something like if(prop == 'side') { if(obj[prop] == 2) { side2.push(key); } Commented Nov 14, 2015 at 3:18

3 Answers 3

1

You want to push when the side value is 2, so you want to check side and not loc. Then you can simply push obj.loc

...
 if (obj.hasOwnProperty(prop)) {
     if(prop == 'side') {
         if(obj[prop] == 2) {
            side2.push(obj.loc);
         }
     }
 }
...

Fiddle Example


That being said you can shorten this code quite a bit, removing unneeded looping and work you can shorten all your code to simply:

for (var key in rooms[room]) {
    var item = rooms[room][key];
    if(item.side == 2) 
        side2.push(item.loc)
}

Fiddle Example

Sign up to request clarification or add additional context in comments.

Comments

0

From your statement, you want to push the value of loc property to the array side2 if the value of side property is 2.

But in your code

if(prop == 'loc') {
   if(obj[prop] == 2) {
      side2.push(key);
   }
}

You are comparing the value of loc property to be 2, not the value of side property. you probably need something like

if(prop == 'side') {
   if(obj[prop] == 2) {
      side2.push(obj['loc']);
   }
}

2 Comments

How is this different/better from just replacing the whole loop with side2.push(obj.side)?
i belive he said he wished to add the value of loc property to the side2 array and not the value of obj.side. And also according to his statement in question He wants to add the vlaue of the property loc to the array (side2). so i don't know why he is adding key to it.
0

As mentioned in comments and other answers, you are looking for a loc property of 2 which doesn't exist. So the immediate problem can be solved by replacing loc with side (assuming that's what you want).

But your code can be simplified. Looping at the top level is fine. However, the entire nested loop part of your code:

for (var prop in obj) {
   if (obj.hasOwnProperty(prop)) {
     if(prop == 'loc') {
       if(obj[prop] == 2) {
         side2.push(key);
       }
     }
   }
}

can be replaced with

if (obj.side == 2) side2.push(key);

In other words, you don't need to loop through an object's properties to find a particular one (side in this case). You can just access it, with obj.side.

You can also conceive of this problem as wanting to filter the list of rooms down to those with people with a loc of 2, in which case you could write:

Object.keys(rooms) . filter(hasPeopleWithLoc(2))

where

function hasPeopleWithLoc(loc) {
  return function(roomKey) {
    var room = rooms[roomKey];
    return Object.keys(room) . some(function(personKey) {
      return room[personKey].loc === loc;
    });
  };
}

Note that this code will leave you with just one entry for a room in the result if anyone in that room has a loc of 2. Your original code behaves slightly differently; it puts an entry for the room in the result for each person in that room with the desired loc.

2 Comments

This is all nice information for the OP to know to remove unnecessary work and shorten their code. But how is this a solution to their problem at hand? The OP isn't looking to add all the values of side into an array.
@SpencerWieczorek No, actually he appears to want to push the room number into the array.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.