How to Convert Nested Arrays Into 1d Array in Javascript
In ES2019, Array.prototype.flat
method was introduced which converts the multi-dimensional array to one-dimensional array upto a defined nested depth.
For Example:
const nestedArr = ["e1", ["e2", "e3", ["e4", "e5", ["e6", "e7"]]]];
// default depth is 1 means it will flated only 1 nested array
nestedArr.flat(); // ["e1", "e2", "e3", ["e4", "e5", ["e6", "e7"]]]
// depth = 2
nestedArr.flat(2); // ["e1", "e2", "e3", "e4", "e5", ["e6", "e7"]]
// for all the way down to the last nested array use Infinity
nestedArr.flat(Infinity); // ["e1", "e2", "e3", "e4", "e5", "e6", "e7"]
The
flat
method is not supported on old version of many browsers.Checkout supported versions here.