DEV Community

Murtaja Ziad
Murtaja Ziad

Posted on • Originally published at blog.murtajaziad.xyz on

How to flatten an array in 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]];
Enter fullscreen mode Exit fullscreen mode

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

let flattenedArray = arrays.flat();
Enter fullscreen mode Exit fullscreen mode

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

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

and yeah, it’s working :D

Top comments (0)