javascript - PokeAPI + Angular: How to get pokemon's evolution chain -


i angular novice , learning little trying pull evolution chain each pokemon using pokeapi having difficult time because of deep nesting.

a typical response object returned 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"     }   } } 

i have evolves_to property, , grab species.name evolution_details.min_level , evolution_details.trigger.name, , evolution_details.item if not null

but can see, evolves_to property, contains evolves_to nested inside, has nested inside

this sad attempt (after http.get) , i'm stuck now.

var evoobject = response.data;  function loopevo(obj){     angular.foreach(obj, function(value, key, object){         if (key == 'evolves_to' && value != []) {             //from here can top level data, but...         }     }); }  loopevo(evoobject.chain); 

i don't know how recursively dive objects , continually grab data, can provide help? love use great learning opportunity in traversing complex json objects.

you avoid using angular , stick plain js build out evolution chain... try giving go, based on angular loop. should leave array (evochain) of objects containing data looking ordered first evolution @ 0 index last evolution @ last index.

var evochain = []; var evodata = response.data.chain;  {   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 sample case above resulting array should appear 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 }] 

Comments

Popular posts from this blog

mysql - Dreamhost PyCharm Django Python 3 Launching a Site -

java - Sending SMS with SMSLib and Web Services -

java - How to resolve The method toString() in the type Object is not applicable for the arguments (InputStream) -