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
Math with Infinity
Infinity is just a number, dude. Most calculations JavaScript lets us do with Infinity will still return Infinity. Some no longer return numbers.
Read fire tipReplacing all matches in a string
The String prototype’s replace function only replaces the first occurrence of a substring by default. We can extend that with a global flag on the expression.
Read fire tip