3

How can I get the "lenght" of "enemies"? I have tried many ways, but I haven´t been able to do it. I want to use the number to loop thru the enemies. I tried Object.key() and many variations, but I don't quiet get how to implemente it, or if I am going on the right direction.

var rooms = {
"start": {
    "description": "You are in a dark, cold place and you see a light to <b>north</b>\
 and you hear the sound of running water to the <b>west</b>",
    "directions": {
        "north": "clearing1",
        "west": "bridge1"
    },
    "enemies": {
      "enemy1": "enemiesDB[0]",
      "enemy2": "enemiesDB[1]"
    }
}

}

UPDATE I change the code to be an array which solved the problem.

    var rooms = {
"start": {
    "description": "You are in a dark, cold place and you see a light to <b>north</b>\
 and you hear the sound of running water to the <b>west</b>",
    "directions": {
        "north": "clearing1",
        "west": "bridge1"
    },
    "enemies": [enemiesDB[0], enemiesDB[1]]
}

Then I was able to use it in a loop like this...

rooms.start.enemies.length

Thanks @NickCordova!

Chris Luna
  • 43
  • 6
  • What do you want "length" to mean? There's no native "length" for plain objects. – Pointy Oct 23 '17 at 18:15
  • Object.keys(rooms.start.enemies).length; – Lixus Oct 23 '17 at 18:15
  • What you are asking for is a duplicate of [How to efficiently count the number of keys/properties of an object in JavaScript?](https://stackoverflow.com/q/126100/218196), but what you actually want to do is a duplicate of [Iterate through object properties](https://stackoverflow.com/q/8312459/218196). Getting the number of properties won't really help you to iterate over them. – Felix Kling Oct 23 '17 at 18:16
  • `"enemiesDB[0]"~ <-- please tell me you are not using eval when you reference that. – epascarello Oct 23 '17 at 18:16
  • I just want to get the number of values contain in "enemies" so I can run a for loop and print the names. – Chris Luna Oct 23 '17 at 18:17
  • @Chris: Iterating over properties doesn't require getting the number of properties. See the duplicate. – Felix Kling Oct 23 '17 at 18:19
  • "enemies[0]" is referencing an object in another array like "enemies = [skeleys, troll, mummy]" – Chris Luna Oct 23 '17 at 18:20
  • @FelixKling I checking out the posts you provided. – Chris Luna Oct 23 '17 at 18:22
  • You'd be better for keeping "enemies" an array so that you can use the built-in array methods. enemies : [ enemiesDB[0], enemiesDB[1] ] – Nick Cordova Oct 23 '17 at 18:28
  • @NickCordova It worked like a charm!!!! – Chris Luna Oct 23 '17 at 18:34

2 Answers2

3

Objects doesn't have a length, however you can use Object.keys to get an array of the enumerable own keys of an object, and read the length of that array.

2

Enemies is an object which does not have a length, i guess you want

Object.keys(rooms.start.enemies).length;

DEMO

var rooms = {
  "start": {
    "description": "You are in a dark, cold place and you see a light  and you hear the sound of running water to the <b>west</b>",
    "directions": {
      "north": "clearing1",
      "west": "bridge1"
    },
    "enemies": {
      "enemy1": "enemiesDB[0]",
      "enemy2": "enemiesDB[1]"
    }
  }
};
console.log(Object.keys(rooms.start.enemies).length);
Sajeetharan
  • 216,225
  • 63
  • 350
  • 396