所以我有这个过滤器,我试图弄清楚我是否可以更容易/更好地做到这一点。只是感觉我重复了很多。我想将所有数字放在一个数组中,然后使用该数组进行过滤。
我可以用包含所有数字的数组来做到这一点吗?像
这样的东西 var filterIDs = [2, 3, 5, 6, 8, 9, 31, ...]
而不是一些代码?我无法弄清楚如何。
它目前有效,但感觉非常笨拙和重复。
let filterPokemonRare = list.filter(function (f) {
return f.pokemon_id == 2 || //Ivysaur
f.pokemon_id == 3 || //Venusaur
f.pokemon_id == 5 || //Charmender
f.pokemon_id == 6 || //Charizard
f.pokemon_id == 8 || //Warturtle
f.pokemon_id == 9 || //Blastoise
f.pokemon_id == 31 || //Nidoqueen
f.pokemon_id == 34 || //Nidoking
f.pokemon_id == 36 || //Clefable
f.pokemon_id == 67 || //Machoke
f.pokemon_id == 68 || //Machamp
f.pokemon_id == 75 || //Graveler
f.pokemon_id == 76 || //Golem
f.pokemon_id == 78 || //Rapidash
f.pokemon_id == 85 || //Dodrio
f.pokemon_id == 89 || //Muk
f.pokemon_id == 94 || //Gengar
f.pokemon_id == 103 || //Exeggutor
f.pokemon_id == 106 || //Hitmonlee
f.pokemon_id == 107 || //Hitmonchan
f.pokemon_id == 108 || //Lickitung
f.pokemon_id == 113 || //Chansey
f.pokemon_id == 114 || //Tangela
f.pokemon_id == 130 || //Gyarados
f.pokemon_id == 131 || //Lapras
f.pokemon_id == 141 || //Kabutops
f.pokemon_id == 142 || //Aerodactyl
f.pokemon_id == 143 || //Snorlax
f.pokemon_id == 148 || //Dragonair
f.pokemon_id == 149 || //Dragonite
//GEN 2
f.pokemon_id == 153 || //Bayleef
f.pokemon_id == 154 || //Meganium
f.pokemon_id == 156 || //Quilava
f.pokemon_id == 157 || //Typhlosion
f.pokemon_id == 159 || //Croconaw
f.pokemon_id == 160 || //Feraligatr
f.pokemon_id == 179 || //Mareep
f.pokemon_id == 180 || //Flaaffy
f.pokemon_id == 181 || //Ampharos
f.pokemon_id == 201 || //UNOWN!!!
f.pokemon_id == 204 || //Pineco
f.pokemon_id == 205 || //Forretress
f.pokemon_id == 231 || //Phanpy
f.pokemon_id == 232 || //Donphan
f.pokemon_id == 237 || //Hitmontop
f.pokemon_id == 241 || //Miltank
f.pokemon_id == 242 || //Blissey
f.pokemon_id == 246 || //Larvitar
f.pokemon_id == 247 || //Pupitar
f.pokemon_id == 248 || //Tyranitar
//GEN 3
f.pokemon_id == 253 || //Grovyle
f.pokemon_id == 254 || //Sceptile
f.pokemon_id == 256 || //Combusken
f.pokemon_id == 257 || //Blaziken
f.pokemon_id == 259 || //Marshtomp
f.pokemon_id == 260 || //Swampert
f.pokemon_id == 271 || //Lombre
f.pokemon_id == 272 || //Ludicolo
f.pokemon_id == 279 || //Pelipper
f.pokemon_id == 281 || //Kirlia
f.pokemon_id == 282 || //Gardevoir
f.pokemon_id == 286 || //Breloom
f.pokemon_id == 288 || //Vigoroth
f.pokemon_id == 289 || //Slaking
f.pokemon_id == 295 || //Exploud
f.pokemon_id == 306 || //Aggron
f.pokemon_id == 308 || //Medicham
f.pokemon_id == 319 || //Sharpedo
f.pokemon_id == 321 || //Wailord
f.pokemon_id == 323 || //Camerupt
f.pokemon_id == 328 || //Trapinch
f.pokemon_id == 329 || //Vibrava
f.pokemon_id == 330 || //Flygon
f.pokemon_id == 332 || //Cacturne
f.pokemon_id == 334 //Altaria
f.pokemon_id == 329 || //Crawdaunt
f.pokemon_id == 346 || //Cradily
f.pokemon_id == 348 || //Armaldo
f.pokemon_id == 357 || //Masquerain
f.pokemon_id == 358 || //Chimecho
f.pokemon_id == 371 || //Bagon
f.pokemon_id == 372 || //Shelgon
f.pokemon_id == 373 || //Salamence
f.pokemon_id == 375 || //Metang
f.pokemon_id == 376; //Metagross
});
答案 0 :(得分:5)
您可以使用数组并检查数组是否包含pokemon_id
。
它适用于所需属性的arrow function,destructuring assignment和Array#includes
来检查pokemon_id
是否是数组filterIDs
的元素。
var filterIDs = [2, 3, 5, 6, 8, 9, 31 /* ... */],
filterPokemonRare = list.filter(({ pokemon_id }) => filterIDs.includes(pokemon_id));
答案 1 :(得分:0)
为了支持更多(较旧的)浏览器,使用一些ES5兼容的东西将是:
let filterPokemonRare = list.filter(function(f) {
var filterIDs = [2, 3, 5, 6, 8, 9, 31, /*...*/]
return filterIDs.indexOf(f.pokemon_id) > -1
})