0

I have an array of objects and within those objects is another object which contains a particular property which I want to get the value from and store in a separate array.

How do I access and store the value from the name property from the data structure below:

pokemon:Object
abilities:Array[2]
  0:Object
    ability:Object
      name:"blaze"
  1:Object
    ability:Object
      name:"solar-power"

How would I return and display the values in the name property as a nice string like

blaze, solar-power ?

I tried doing something like this but I still get an array and I don't want to do a 3rd loop since that is not performant.

 let pokemonAbilities = [];
 let test = pokemon.abilities.map((poke) =>
        Object.fromEntries(
          Object.entries(poke).map(([a, b]) => [a, Object.values(b)[0]])
        )
      );

      test.map((t) => pokemonAbilities.push(t.ability));

Sample Data:

"pokemon": {
  "abilities": [
    {
      "ability": {
        "name": "friend-guard",
        "url": "https://pokeapi.co/api/v2/ability/132/"
      },
      "ability": {
        "name": "Solar-flare",
        "url": "https://pokeapi.co/api/v2/ability/132/"
      }
    }
  ]
}

Then I am doing a join on the returned array from above to get a formatted string.

It just seems like the multiple map() loops can be optimized but I am unsure how to make it more efficient.

Thank you.

4
  • what does the data actually look like? I mean, put something in the code block that we can use directly in the solution. Commented Apr 29, 2022 at 22:07
  • 1
    @ChrisStrickland there you go, sir. Posted some real data formatted to work with. Commented Apr 29, 2022 at 22:10
  • @PA-GW Are you sure that given data is correct? Array named abilities has one object with 2 same name (ability) properties. I think you wanted 2 objects with where both of them has property named ability Commented Apr 29, 2022 at 22:25
  • 1
    the sample data you gave is incorrectly formatted. abilities should be an array of objects, each object containing a key "ability" with "name" and "url". you didn't reproduce it correctly from the API sample. see answer below for the correct format. Commented Apr 29, 2022 at 22:50

1 Answer 1

2

There is no need for a loop within loop. Try this:

const pokemon = {
  abilities: [{
    ability: {
      name: 'friend-guard',
      url: 'https://pokeapi.co/api/v2/ability/132/'
    },
  }, {
    ability: {
      name: 'Solar-flare',
      url: 'https://pokeapi.co/api/v2/ability/132/'
    }
  }]
};

const pokemonAbilities = pokemon.abilities.map(item => item.ability.name).join(', ');

console.log(pokemonAbilities);

Sign up to request clarification or add additional context in comments.

6 Comments

Data type you used in your answer not same data from question. Objects inside the object in question
@Evren You are right! But I posted my answer before he gave real data. I just didn't have time to fix :)
Yes sorry the code above doesn't work because abilities has nested ability object inside of it.
@PA-GW Couple of minutes pls :)
Positive, you can see the docs for yourself pokeapi.co/docs/v2
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.