JavaScript Array.flat()
Rajika Imal
Posted on October 30, 2020
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]
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]]]
const array = [1, 2, 3, 4, [[[5, 6]]]]
const flatArray = array.flat(3) // [1, 2, 3, 4, 5, 6]
💖 💪 🙅 🚩
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
webdev JavaScript Higher-Order Functions Made Easy: Learn with a Real-Life Example! 💡
November 30, 2024