Javascript - .flat() and .flatMap()
Code Life
By Pierre Marsaa
Flattening Nested Arrays
Imagine your Codémon storage boxes are organized as an array of smaller sub-arrays. If you want to gather every Codémon from every box into a single simple array, you can use .flat().
The .flat() method removes the inner array brackets and returns a new array containing all the elements in one single list:
↗️
Loading editor...
↶
↷
Here, boxes had 3 sub-arrays, and boxes.flat() unpacked them into a new array of 5 Codémon without modifying boxes.
Verify