-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathflattenArray.js
More file actions
executable file
·36 lines (31 loc) · 830 Bytes
/
flattenArray.js
File metadata and controls
executable file
·36 lines (31 loc) · 830 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
36
function flattenArray(arr) {
let newArray = [];
arr.forEach(element => {
if (Array.isArray(element)) {
newArray = [...newArray, ...flattenArray(element)];
} else {
newArray.push(element);
}
});
return newArray;
}
function flattenArrayReduce(arr) {
return arr.reduce((acc, element) => {
if (Array.isArray(element)) return [...acc, ...flattenArray(element)];
acc.push(element);
return acc;
}, []);
}
console.log(flattenArray([1, [[2, [3, [4, [5]]]]]]));
console.log(flattenArrayReduce([1, [[2, [3, [4, [5]]]]]]));
Function.prototype.bind = function(context) {
const _thisFunction = this;
return function(args = []) {
return _thisFunction.apply(context, [...args]);
};
};
function foo() {
console.log(this.bar);
}
baz = foo.bind({ bar: "hello" });
console.log(baz());