-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathflat.js
33 lines (29 loc) · 1.32 KB
/
flat.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
'use strict';
// TOPIC /////////////////////////////////////////////////////////////////////////////////////////////////////
//
// Array Method: .flat()
//
// SYNTAX ////////////////////////////////////////////////////////////////////////////////////////////////////
//
// let newArray = array.flat(depth);
//
// SUMMARY ///////////////////////////////////////////////////////////////////////////////////////////////////
//
// • .flat() creates a new array with all the sub-arrays element concatenated into it recursively
// up to the specified depth.
// • .flat() is an ES2019 addition, so compatibility is still weak, especially with IE or edge.
// • .flat() takes as a parameter the depth to which you can flatten the array.
//
// EXAMPLES //////////////////////////////////////////////////////////////////////////////////////////////////
//
// EXAMPLE 1: ES6 work-around
//
// RESOURCES /////////////////////////////////////////////////////////////////////////////////////////////////
//
//////////////////////////////////////////////////////////////////////////////////////////////////////////////
// const missingNumbers = [1, ,3, ,5];
// missingNumbers.flat();
// ES6 work-around solution.
const oneLevelDeep = [[1, 2], [3]];
const flattened = [].concat(...oneLevelDeep);
console.log(flattened);