3

I have this object and I need a function to filter the pokemon (whole objects) by type or weaknesses (the user decides). For example: "the user needs to filter all the fire type pokemon" and the result would be an array containing every fire type pokemon object

var POKEMON = {
  "pokemon": [{
    "id": 1,
    "num": "001",
    "name": "Bulbasaur",
    "img": "https://assets.pokemon.com/assets/cms2/img/pokedex/full/001.png",
    "type": [
      "Grass",
      "Poison"
    ],
    "height": "0.71 m",
    "weight": "6.9 kg",
    "candy": "Bulbasaur Candy",
    "candy_count": 25,
    "egg": "2 km",
    "spawn_chance": 0.69,
    "avg_spawns": 69,
    "spawn_time": "20:00",
    "multipliers": [1.58],
    "weaknesses": [
      "Fire",
      "Ice",
      "Flying",
      "Psychic"
    ],
    "next_evolution": [{
      "num": "002",
      "name": "Ivysaur"
    }, {
      "num": "003",
      "name": "Venusaur"
    }]
  },{
    "id": 2,
    "num": "002",
    "name": "Ivysaur",
    "img": "https://assets.pokemon.com/assets/cms2/img/pokedex/full/002.png",
    "type": [
      "Grass",
      "Poison"
    ],
    "height": "0.99 m",
    "weight": "13.0 kg",
    "candy": "Bulbasaur Candy",
    "candy_count": 100,
    "egg": "Not in Eggs",
    "spawn_chance": 0.042,
    "avg_spawns": 4.2,
    "spawn_time": "07:00",
    "multipliers": [
      1.2,
      1.6
    ],
    "weaknesses": [
      "Fire",
      "Ice",
      "Flying",
      "Psychic"
    ],
    "prev_evolution": [{
      "num": "001",
      "name": "Bulbasaur"
    }],
    "next_evolution": [{
      "num": "003",
      "name": "Venusaur"
    }]
  },
... (+149 other pokemon)

I already have a function and it works but I'd rather not use for loops:

const filterPokemon = (data, whatToFilter, valueToCompare) => {  
  return data.filter(pokemon => {
    for(let i = 0 ; i < pokemon[whatToFilter].length ; i++){
      if(pokemon[whatToFilter][i] === valueToCompare){       
        return pokemon;
      }
    }
  });
};

filterPokemon(POKEMON.pokemon, "type", "Fire");

3 Answers 3

2

Use .includes instead to see if any items in the pokemon[whatToFilter] array equal the valueToCompare:

const filterPokemon = (data, whatToFilter, valueToCompare) => {  
  return data.filter(creatureObj => (
    creatureObj[whatToFilter].includes(valueToCompare)
  ));
};

Side note, might be a bit opinion-based, but you might consider changing the variable name from pokemon (eg here I used "creatureObj") because "pokemon" can be either singular or plural, so it isn't entirely clear what sort of object it is.

Could also avoid that entirely by destructuring the parameter, if you wanted:

const filterPokemon = (data, whatToFilter, valueToCompare) => {  
  return data.filter(({ [whatToFilter]: arr }) => (
    arr.includes(valueToCompare)
  ));
};
Sign up to request clarification or add additional context in comments.

Comments

1

You have the first part right, but just use the array find() operator to filter, or since these are simple primitive arrays, you could just use .indexOf(value) > -1 or .includes() or a few other operators. I prefer find because it works on complex types as well.

const filterPokemon = (pokemonList, propToFilter, value) => {  
  return pokemonList.filter(pokemon => {
    return pokemon[propToFilter].find(p => p === value);
  });
};

Comments

1

Another option is to use Array.some() inside the filter function.

const filterPokemon = (data, whatToFilter, valueToCompare) =>
{  
  return data.filter(pokemon => pokemon[whatToFilter].some(x => x === valueToCompare));
}

Example:

var POKEMON = {
  "pokemon": [{
    "id": 1,
    "num": "001",
    "name": "Bulbasaur",
    "img": "https://assets.pokemon.com/assets/cms2/img/pokedex/full/001.png",
    "type": [
      "Grass",
      "Poison"
    ]
  },
  {
    "id": 2,
    "num": "002",
    "name": "Ivysaur",
    "img": "https://assets.pokemon.com/assets/cms2/img/pokedex/full/002.png",
    "type": [
      "Grass",
      "Poison",
      "Fire"
    ],
  }
]};

const filterPokemon = (data, whatToFilter, valueToCompare) =>
{  
    return data.filter(pokemon => pokemon[whatToFilter].some(x => x === valueToCompare));
}
    
let res = filterPokemon(POKEMON.pokemon, "type", "Fire");
console.log(res);
.as-console {background-color:black !important; color:lime;}
.as-console-wrapper {max-height:100% !important; top:0;}

Comments

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.