Skip to main content

Array.flat

if (!Array.prototype.flat) {
Array.prototype.flat = function (depth = 1) {
let flatten = (arr, depth) => {
return depth > 0
? arr.reduce(
(acc, val) =>
acc.concat(Array.isArray(val) ? flatten(val, depth - 1) : val),
[]
)
: arr.slice();
};
return flatten(this, depth);
};
}