JavaScript Array.flat()

rajikaimal

Rajika Imal

Posted on October 30, 2020

JavaScript Array.flat()

Arrays with embedded sub arrays can be concatenated using Array.flat()

const array = [1, 2, 3, 4, [5, 6]]
const flatArray = array.flat() // [1, 2, 3, 4, 5, 6]
Enter fullscreen mode Exit fullscreen mode

The depth can be specified using an argument. Array.flat(depth)

const array = [1, 2, 3, 4, [[[5, 6]]]]
const flatArray = array.flat(1) // [1, 2, 3, 4, [[5, 6]]]
Enter fullscreen mode Exit fullscreen mode
const array = [1, 2, 3, 4, [[[5, 6]]]]
const flatArray = array.flat(3) // [1, 2, 3, 4, 5, 6]
Enter fullscreen mode Exit fullscreen mode
💖 💪 🙅 🚩
rajikaimal
Rajika Imal

Posted on October 30, 2020

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related