How to flatten an array in JavaScript?

January 22, 2021#javascript

In this article, you will learn how to flat an array in JavaScript.

That’s the array of arrays that we have:

let arrays = [[1, 2], [3, 4], [5]];

You can flat it easily using .flat() method and assigning it to a new variable:

let flattenedArray = arrays.flat();

If you want to see the flattened array, just log it to the console:

console.log(flattenedArray); // [1, 2, 3, 4, 5]

and yeah, it's working :D