How To Flat an Array in JavaScript

const array = [1, [2, [3, [4, 5, 6]]]]

array.flat()  // == array.flat(1)
// [1, 2, [3, [4, 5, 6]]]

array.flat(Infinity)
// [1, 2, 3, 4, 5, 6]

Array.prototype.flatMap combines flat and map

const duplicate = x => [x, x];

[2, 3, 4].map(duplicate);
// [[2, 2], [3, 3], [4, 4]]

[2, 3, 4].map(duplicate).flat();

// or

[2, 3, 4].flatMap(duplicate)
Copyright © 2019 All rights reserved.
Powered by Kulfon.