Flattening an array involves reducing its dimensionality.
This process is beneficial when converting a multi-dimensional array into a one-dimensional array.
ES2019 introduced the Array flat() method.
The flat() method generates a new array by concatenating sub-array elements up to a specified depth.
Example
const myArr = [[1,2],[3,4],[5,6]]; const newArr = myArr.flat(); |
The JavaScript Array flat() method is supported in all modern browsers as of January 2020.
The splice() method inserts new items into an array.
Meanwhile, the slice() method extracts a portion of an array.