简体   繁体   English

循环遍历对象中的数组

[英]Going through arrays in object with a loop

I got several different arrays inside an object. 我在对象内部有几个不同的数组。 I would like to go trough them with numeral value as I need to set them in a loop to different slots in accordion. 我想使用数字值浏览它们,因为我需要将它们循环设置为手风琴中的不同插槽。 Json file I'm getting the value looks kinda like this (Pokemon used for example): 我得到的Json文件的值看起来像这样(例如,使用Pokemon):

{
  "Pokemon": {
    "FirePokemon": [
      {
        "name": "Vulpix",
        "speed": "10",
        "attack": "10",
        "defence": "10"
      },
      {
        "name": "Charmander",
        "speed": "10",
        "attack": "10",
        "defence": "10"
      }
    ],
    "WaterPokemon": [
      {
        "name": "Squirtle",
        "speed": "10",
        "attack": "10",
        "defence": "10"
      },
      {
        "name": "Wartortle",
        "speed": "10",
        "attack": "10",
        "defence": "10"
      }
    ],
    "GrassPokemon": [
      {
        "name": "Bulbasaur",
        "speed": "10",
        "attack": "10",
        "defence": "10"
      },
      {
        "name": "Oddish",
        "speed": "10",
        "attack": "10",
        "defence": "10"
      }
    ]
  }
}

I would like to call the data something like this: 我想这样称呼数据:

function SetJsonDataToAccordion() {
    for (var i = 0; i < Object.keys(pokemondata).length; i++) {
        CreateAccordionContent(pokemondata[i], ".accordtitle"+i);
    }
}

pokemondata is the variable that gets all the Json data. pokemondata是获取所有Json数据的变量。 Of course the pokemondata[i] does not work, but I would like to cycle the pokemon types like that swapping fire then water then grass, etc. in the loop without calling the arrays names. 当然,pokemondata [i]不起作用,但是我想在循环中循环调用pokemon类型,例如在不调用数组名称的情况下在循环中依次交换火,水,草等。 It all works if I just set it pokemondata.FirePokemon but I need to loop trough them. 如果我只是将其设置为pokemondata.FirePokemon,那么一切都可以,但是我需要循环通过它们。 So is there a way you can loop trough the arrays in an object? 那么有没有一种方法可以循环遍历对象中的数组?

You can use for / in loop. 您可以使用for / in循环。

Based on your example: You need 3 nested loops. 根据您的示例:您需要3个嵌套循环。

 var obj = { "Pokemon": { "FirePokemon": [{ "name": "Vulpix", "speed": "10", "attack": "10", "defence": "10" }, { "name": "Charmander", "speed": "10", "attack": "10", "defence": "10" } ], "WaterPokemon": [{ "name": "Squirtle", "speed": "10", "attack": "10", "defence": "10" }, { "name": "Wartortle", "speed": "10", "attack": "10", "defence": "10" } ], "GrassPokemon": [{ "name": "Bulbasaur", "speed": "10", "attack": "10", "defence": "10" }, { "name": "Oddish", "speed": "10", "attack": "10", "defence": "10" } ] } }; for (var key1 in obj.Pokemon) { console.log("======================="); console.log(key1); console.log("======================="); for (var key2 in obj.Pokemon[key1]) { for (var key3 in obj.Pokemon[key1][key2]) { console.log(key3 + ": " + obj.Pokemon[key1][key2][key3]); } console.log("****"); } } 

Iterate through both. 遍历两者。 With Object.keys you can get the list of pokemon types and with that information you can iterate through each pokemon. 使用Object.keys,您可以获取宠物小精灵类型的列表,并可以通过这些信息遍历每个宠物小精灵。

var pokemonData = obj.Pokemon;
Object.keys(pokemonData).forEach(type => {
   // do something with pokemon type
   pokemonData[type].forEach(pokemon => {
      //do something with actual pokomen
   });
});

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

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