Flattening nested arrays
Array.prototype.flat()
can un-nest nested arrays. It can remove only some levels, or all of them if we tell it to go infinitely many levels deep.
// each value in this array is also how many levels deep it is nested
const nested = [ 0, [1], [[2]], [[[3]]], [[[[[[[[[9]]]]]]]]] ]
// the parameter given to `flat` is how many levels of nesting it removes
nested.flat(0) // ⇒ [ 0, [1], [[2]], [[[3]]], [[[[[[[[[9]]]]]]]]] ]
nested.flat(1) // ⇒ [ 0, 1, [2], [[3]], [[[[[[[[9]]]]]]]] ]
nested.flat(2) // ⇒ [ 0, 1, 2, [3], [[[[[[[9]]]]]]] ]
nested.flat(Infinity) // ⇒ [ 0, 1, 2, 3, 9 ]
More fire tips
Catching unknown keys in lookup tables
Lookup objects can replace long if-else branches and switch statements. We can make them more resilient by checking if a value exists for a given key.
Read fire tip