DEV Community

Mehul Lakhanpal
Mehul Lakhanpal

Posted on • Originally published at codedrops.tech

Array.flat()

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

console.log(array.flat(1)); // [ 1, 2, [ 3, 4, [ 5, 6 ] ], 7 ]
console.log(array.flat(2)); // [ 1, 2, 3, 4, [ 5, 6 ], 7 ]
console.log(array.flat(3)); // [ 1, 2, 3, 4, 5, 6, 7 ]
Enter fullscreen mode Exit fullscreen mode

.flat() (ES2019) will flatten an array up to the given depth level.


Thanks for reading 💙

Follow @codedrops.tech for daily posts.

InstagramTwitterFacebook

Micro-Learning ● Web Development ● Javascript ● MERN stack ● Javascript

codedrops.tech

Discussion (0)