PokeAPI + Angular:如何获得口袋妖怪的进化链 [英] PokeAPI + Angular: How to get pokemon's evolution chain

查看:50
本文介绍了PokeAPI + Angular:如何获得口袋妖怪的进化链的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是一个Angular新手并且通过尝试使用pokeapi拉动每个口袋妖怪的进化链但是因为深度嵌套而度过了困难时间而学习了一点。

I am an Angular novice and am learning a little by trying to pull the evolution chain for each pokemon using pokeapi but having a difficult time because of deep nesting.

典型的响应对象返回如下:

A typical response object is returned like this:

{
  "baby_trigger_item": null,
  "id": 2,
  "chain": {
    "evolution_details": [],
    "evolves_to": [
      {
        "evolution_details": [
          {
            "min_level": 16,
            "min_beauty": null,
            "time_of_day": "",
            "gender": null,
            "relative_physical_stats": null,
            "needs_overworld_rain": false,
            "turn_upside_down": false,
            "item": null,
            "trigger": {
              "url": "http://pokeapi.co/api/v2/evolution-trigger/1/",
              "name": "level-up"
            },
            "known_move_type": null,
            "min_affection": null,
            "party_type": null,
            "trade_species": null,
            "party_species": null,
            "min_happiness": null,
            "held_item": null,
            "known_move": null,
            "location": null
          }
        ],
        "evolves_to": [
          {
            "evolution_details": [
              {
                "min_level": 36,
                "min_beauty": null,
                "time_of_day": "",
                "gender": null,
                "relative_physical_stats": null,
                "needs_overworld_rain": false,
                "turn_upside_down": false,
                "item": null,
                "trigger": {
                  "url": "http://pokeapi.co/api/v2/evolution-trigger/1/",
                  "name": "level-up"
                },
                "known_move_type": null,
                "min_affection": null,
                "party_type": null,
                "trade_species": null,
                "party_species": null,
                "min_happiness": null,
                "held_item": null,
                "known_move": null,
                "location": null
              }
            ],
            "evolves_to": [],
            "is_baby": false,
            "species": {
              "url": "http://pokeapi.co/api/v2/pokemon-species/6/",
              "name": "charizard"
            }
          }
        ],
        "is_baby": false,
        "species": {
          "url": "http://pokeapi.co/api/v2/pokemon-species/5/",
          "name": "charmeleon"
        }
      }
    ],
    "is_baby": false,
    "species": {
      "url": "http://pokeapi.co/api/v2/pokemon-species/4/",
      "name": "charmander"
    }
  }
}

我必须得到evolves_to属性,并抓住species.name如evolution_details.min_level和evolution_details.trigger.name,以及evolution_details.item如果不为空

I have to get to evolves_to property, and grab species.name as well as evolution_details.min_level and evolution_details.trigger.name, and evolution_details.item if not null

但是正如你所看到的,evolves_to属性本身包含另一个evolves_to嵌套在里面,其中有另一个嵌套在里面

But as you can see, the evolves_to property, itself contains another evolves_to nested inside, which has another nested inside

这是我的悲惨尝试(在http.get之后),我现在只是卡住了。

This is my sad attempt (after http.get) and I'm just stuck now.

var evoObject = response.data;

function loopEvo(obj){
    angular.forEach(obj, function(value, key, object){
        if (key == 'evolves_to' && value != []) {
            //from here I can get top level data, but...
        }
    });
}

loopEvo(evoObject.chain);

我不知道如何递归地潜入对象并不断获取数据,任何人都可以提供任何帮助?我希望在遍历复杂的json对象时使用它作为一个很好的学习机会。

I don't know how to recursively dive into objects and continually grab data, can anyone provide any help? I would love to use this as a great learning opportunity in traversing complex json objects.

推荐答案

你总是可以避免使用Angular和坚持使用简单的JS来构建你的进化链......尝试给它一个去,它是基于你的角度循环。这应该为您提供一个数组( evoChain )包含您要查找的数据的对象,从0索引处的第一个进化到最后一个索引的最后进化。

You could always just avoid using Angular and stick with plain JS to build out your evolution chain... try giving this a go, it was based on your angular for loop. This should leave you with an array (evoChain) of the objects containing the data you are looking for ordered from first evolution at 0 index to last evolution at the last index.

var evoChain = [];
var evoData = response.data.chain;

do {
  var evoDetails = evoData['evolution_details'][0];

  evoChain.push({
    "species_name": evoData.species.name,
    "min_level": !evoDetails ? 1 : evoDetails.min_level,
    "trigger_name": !evoDetails ? null : evoDetails.trigger.name,
    "item": !evoDetails ? null : evoDetails.item
  });

  evoData = evoData['evolves_to'][0];
} while (!!evoData && evoData.hasOwnProperty('evolves_to'));

在上面的示例中,结果数组应如下所示:

In your sample case above the resulting array should appear as follows:

[{
    "species_name": "charmander",
    "min_level": 1,
    "trigger_name": null,
    "item": null
}, {
    "species_name": "charmeleon",
    "min_level": 16,
    "trigger_name": "level-up",
    "item": null
}, {
    "species_name": "charizard",
    "min_level": 36,
    "trigger_name": "level-up",
    "item": null
}]

这篇关于PokeAPI + Angular:如何获得口袋妖怪的进化链的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆