简体   繁体   English

在JavaScript中循环遍历对象内部的数组

[英]Loop through array inside an object in JavaScript

I've got this array 我有这个数组

var enemy = {
'level1' : {
    creature :
    {
        creature_name : {
             'Goblin' : {
                info: {
                    'c_name' : 'Goblin',
                    'HP' : '20',
                    'damage' : '3',
                    'loot' : [
                        {name: 'a wooden sword'   , item: 'weapon'  , value: 2}, 
                        {name: 'a golden necklace', item: 'amulet' , value: 1},
                        {name: 'a pair of boots'  , item: 'boots'  , value: 1},
                        {name: 'some cloth legs'  , item: 'legs'  , value: 1},
                        {name: 'a cloth helmet'   , item: 'helm'  , value: 1}
                    ]
                }
             },
             'Cow' : {
                info: {
                    'c_name' : 'Cow',
                    'HP' : '10',
                    'damage' : '1',
                    'loot' : [
                        {name: 'bell'              , item: 'weapon'  , value: 0}, 
                        {name: 'cow hide cloak'    , item: 'cape'  , value: 1}, 
                        {name: 'a wooden sword'    , item: 'weapon'  , value: 2}, 
                        {name: 'a golden necklace' , item: 'amulet' , value: 1},
                    ]
                }
             },
             'Dragon' : {
                info: {
                    'c_name' : 'Dragon',
                    'HP' : '100',
                    'damage' : '5',
                    'loot' : [
                        {name: 'an almighty dragon sword'   , item: 'weapon'  , value: 5}, 
                        {name: 'a dragon tooth', item: 'amulet' , value: 5},
                        {name: 'a pair of dragon boots'  , item: 'boots'  , value: 4},
                        {name: 'a dragon helmet'  , item: 'helm'  , value: 4}
                    ]
                }
             },

        }

    },
},

I want to receive the _creature_name_ . 我想收到_creature_name_ I saw foreach loops with arrayname.length but when I try to do enemy.length or enemy.level1.creature.creature_name.length then I get undefined . 我看到了带有arrayname.length foreach循环,但是当我尝试执行enemy.lengthenemy.level1.creature.creature_name.length我变得undefined

Objects (key/value pairs) don't have an intrinsic .length property. 对象(键/值对)没有固有的.length属性。

To just get the names as an array you could use: 要将名称作为数组获取,可以使用:

var names = Object.keys(enemy.level1.creature.creature_name);

or alternatively to just iterate directly over each name: 或者只是直接遍历每个名​​称:

for (var name in enemy.level1.creature.creature_name) {
    ...
}

Regarding the actual content you're after, you could use: 关于您所追求的实际内容,您可以使用:

$('#enemy_list').html('Enemies: <br/>Level1: <br/>' +
    Object.keys(enemy.level1.creature.creature_name).join('<br/>'));

or: 要么:

var content = ['Enemies:', 'Level1:'].concat(Object.keys(enemy.level1.creature.creature_name));
$('#enemy_list').html(content.join('<br/>'));

Since objects don't have length properties assigned to the number of properties inside one solution would be to loop classically through the object as following: 由于没有将对象的长度属性分配给一个解决方案中的属性数,因此经典地遍历对象如下:

for(var prop in enemy['level1'].creature.creature_name){
   console.log(prop)
}

This will print out the name one by one 这将一一打印出名称

I prepared it in a jsfiddle also: https://jsfiddle.net/fo8xjr8w/ 我也在jsfiddle中准备了它: https ://jsfiddle.net/fo8xjr8w/

var creature_name = enemy.level1.creature.creature_name;

for (var prop in creature_name)
{   
    console.log(prop);
    //this would print Goblin, Cow, Dragon
}

You again have to iterate if you want to traverse further internal details for the loot array. 如果要遍历战利品数组的更多内部细节,则必须再次进行迭代。

$('#enemy_list')[0].innerHTML = 'Enemies: <br />Level 1: <br />';
for(var enemy_names in enemy['level1'].creature.creature_name){
    $('#enemy_list')[0].innerHTML += enemy_names + '<br />';
}

You need create a prop for the name in enemy['level1'].creature.creature_name and then foreach enemy_name place it in the containing div. 您需要在enemy['level1'].creature.creature_name为该名称创建一个道具,然后让每个enemy['level1'].creature.creature_name名称将其放置在包含的div中。 Goodluck! 祝好运!

By the way, your problem with the for loop was that you executed the enemies: <br /> level1: <br /> in your for loop aswell so it would display it 3 times. 顺便说一句,在for循环的问题是,你执行的enemies: <br /> level1: <br />for loop藏汉所以它会显示它的3倍。 This method you put the names inside the div containing the text only once. 通过这种方法,您可以将名称仅包含一次的div放入div中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM