2

So I have this filter, I was trying to figure out if I could do this easier/better. It just feels I repeat myself so much. I would like to put all the numbers in an array, and then use that array to filter.

So can I do this with an array which includes all the numbers? Something like

var filterIDs = [2, 3, 5, 6, 8, 9, 31, ...]

And than some code? I just can't figure out how.

It works at the moment, but it feels so clumsy and repetitive.

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

2 Answers 2

5

You could use an array and check if the array includes the pokemon_id.

It works with an arrow function, a destructuring assignment for the wanted property and Array#includes to check if the pokemon_id is an element of the array filterIDs.

var filterIDs = [2, 3, 5, 6, 8, 9, 31 /* ... */],
    filterPokemonRare = list.filter(({ pokemon_id }) => filterIDs.includes(pokemon_id));
Sign up to request clarification or add additional context in comments.

3 Comments

Thank you! I try to understand what is happening. Can I also use comparison operators in the includes? Like != filterIDs? So I can get all except the ones in the array? list.filter(({ pokemon_id }) => otherFilterIDs.includes(!=pokemon_id));
you mean the get the inversed result, includes accepts only a value, but you could use !, logical NOT operator in the callback of the filter: list.filter(({ pokemon_id }) => !otherFilterIDs.includes(pokemon_id))
That actually makes much sense. Thank you!
0

Using something a little more ES5 compatible in order to support more (older) browsers would be:

let filterPokemonRare = list.filter(function(f) {
  var filterIDs = [2, 3, 5, 6, 8, 9, 31, /*...*/]
  return filterIDs.indexOf(f.pokemon_id) > -1
})

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.