Flat() Method in JavaScript 🔥 #javascript #DSA #javascriptinterview

preview_player
Показать описание
The flat() method in Javascript creates a new array with all the sub-array elements concatenated into it recursively up to a specified depth. If no depth is provided, it defaults to 1.

Here's an example:

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

// Using flat() with default depth (1)
// Output: [1, 2, 3, 4, [5, 6]]

// Using flat() with depth 2
// Output: [1, 2, 3, 4, 5, 6]
Рекомендации по теме
Комментарии
Автор

It's good to do it using recursion

DevAspire
Автор

Hii.. What about n levels?? For example 100 levels.? So try this --- let arr = [1, [2, [2, 3, [1, [2], 3]]], [4, 5]]; let flatArr = arr.flat(Infinity);

helogudmorning
Автор

Bro Use flat (Infinity ) or use mapflat

rakeshsaha