-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFlatten-an-array.js
More file actions
35 lines (29 loc) · 812 Bytes
/
Flatten-an-array.js
File metadata and controls
35 lines (29 loc) · 812 Bytes
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
34
35
/**
* Flattenning an array with builtin methods and
* without builtin methods
*/
var arr = [1, [2, 3], [4, 5, 6, [7, 8, [9, 10]]], 11, 12];
// Its an experimental feature so you can not use it as of now.
console.log(arr.flat());
// Without using builtin methods
function flattenArray(arr) {
let newArr = [];
for (let i = 0; i < arr.length; i++) {
if (Array.isArray(arr[i])) {
newArr = newArr.concat(flattenArray(arr[i]));
} else {
newArr.push(arr[i]);
}
}
return newArr;
}
console.log(flattenArray(arr));
// Using reduce methods
function flattenUsingReduce(arr) {
return arr.reduce((result, currentVal) => {
return result.concat(
Array.isArray(currentVal) ? flattenUsingReduce(currentVal) : currentVal
);
}, []);
}
console.log(flattenUsingReduce(arr));