Removing specific values from an array


Going through Set is cool to filter out duplicates. If you need to filter out specific values instead, you can use this helper.

// takes an array of values and a second array of values to take out of it
const without = (array, valuesToRemove) => {
  return array.filter(value => !valuesToRemove.includes(value))

// all instances of the values to filter out will be removed
without([0, 8, 12, 15, 4, 8, 15, 23], [8, 15])        // ⇒ [0, 12, 4, 23]

// using `.split()` and `.join()`, we can quickly filter values from strings
without('↑↑↓↓←→←→BA'.split(''), ['←', '↑']).join('')  // ⇒ '↓↓→→BA'

More fire tips

Splitting arrays into chunks

By combining map and slice, we can write a helper function to split large arrays into many similarly sized blocks. This is useful for features like pagination.

Read fire tip

Read all fire tips →