-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdeepFlattenArray.js
50 lines (47 loc) · 1.49 KB
/
deepFlattenArray.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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
/**
* Using recursion and JS-style to write a more compact deep array's flattener.
*/
function deepFlattenArrayJSStyle(array) {
const recursiveFlattener = arr => [].concat(...arr.map(v => (Array.isArray(v) ? recursiveFlattener(v) : v)));
return recursiveFlattener(array);
}
/**
* Take an array and flatten it.
* Deals with deep leveled arrays.
* [3, 4, [5, 6, [7, 8]]]
*/
function deepFlattenArrayNaive(array) {
var array = [...array];
while(containsSubArray(array)) { // the simple idea is: re-flatten as long as not entirely flatten
array = flattenArray(array);
}
return array;
}
function containsSubArray(array) {
for(let i=0; i<array.length; i++) {
if(Array.isArray(array[i])) {
return true;
}
}
return false;
}
function flattenArray(array) {
for(let i=0; i<array.length; i++) { // run through the array
if(Array.isArray(array[i])) { // if an element is an array itself
let subArray = array.splice(i, 1)[0]; // then take this sub-array
let s = i;
for(let index = 0; index<subArray.length; index++) {
array.splice(s++, 0, subArray[index]); // add elements of sub-array to array in the position of sub-array
}
}
}
return array;
}
/**
* Example of use
*/
let array = [3, 4, [5, 6, [7, 8, [9, 10]]]];
let flattened = deepFlattenArrayNaive(array);
let flattenedJS = deepFlattenArrayJSStyle(array);
console.log(flattened);
console.log(flattenedJS);