我该如何获取“enemies”的“长度”? 我尝试了很多方法,但都没有成功。 我想使用这个数字来循环遍历敌人。 我尝试了Object.key()和许多变化,但我不太清楚如何实现它,或者我是否朝着正确的方向前进。
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]"
}
}
}
更新 我把代码改成了一个数组,这解决了问题。
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]]
}
然后我就可以像这样在循环中使用它...
rooms.start.enemies.length
感谢 @NickCordova!