在javascript数组内的对象内过滤数组

时间:2019-02-28 02:43:42

标签: javascript arrays object methods filter

我有这个对象,我需要一个功能来按类型或弱点(用户决定)过滤口袋妖怪(整个对象)。 例如:“用户需要过滤所有射击类型的宠物小精灵”,结果将是一个包含每个射击类型的宠物小精灵对象的数组

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)

我已经有一个函数并且可以工作,但是我不想使用for循环:

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 个答案:

答案 0 :(得分:2)

使用.includes来查看pokemon[whatToFilter]数组中是否有任何项等于valueToCompare

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

旁注,可能有点基于意见,但是您可以考虑从pokemon更改变量名(例如,在这里我使用了“ creatureObj”),因为“ pokemon”可以是单数或复数,因此尚不清楚它是哪种对象。

如果需要,还可以通过破坏参数来完全避免这种情况:

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

答案 1 :(得分:1)

您拥有第一部分的权利,但是只需使用数组find()运算符进行过滤,或者由于这些是简单的原始数组,您可以仅使用.indexOf(value)> -1或.includes()或几个其他运营商。我更喜欢找到它,因为它也适用于复杂类型。

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

答案 2 :(得分:1)

另一种选择是在filter函数中使用 Array.some()

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

示例:

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;}