Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions JavaScript_Advance/arrayFlat.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
const array = [];
array.flat(); // This is array flat method came new in ES-2019


/**
* The flat() method creates a new array with all sub-array elements concatenated into
* it recursively up to the specified depth.
*
*/

const arr = [[1, 2, 3], ['a', 'b', 'c']]

console.log(arr.flat()) // [1,2,3,'a','b','c']

const arr1 = [[1, 2, 3], ['a', ['b', 'c']]]

// My default flat only work one level deep
console.log(arr1.flat()) // [1,2,3,'a', ['b','c']]

// if you want to work on more level you can pass level etc. 2, 3
console.log(arr1.flat(2)) // [1,2,3,'a', 'b','c']

// if you want to work on n level you need to pass Infinity
console.log(arr1.flat(Infinity)) // [1,2,3,'a', 'b','c']
1 change: 1 addition & 0 deletions docs/JavaScript_Advance/arrayFlat.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# Array Flat Method