let arr0 = [1, 3, 4, 5, [1]];
let arr1 = [1, 2, 3, [1, [2, { a: 3 }], 4, [2, 3, 4]]];
function flatten(input) {
const stack = [...input];
const res = [];
let i = 0;
while (stack.length) {
const next = stack.pop();
if (Array.isArray(next)) {
stack.push(...next);
} else if (typeof next === 'object') {
res.push(...Object.values(next));
} else {
res.push(next);
}
}
return res.reverse();
}
flatten(arr0);
flatten(arr1);
Last active
August 19, 2021 08:33
-
-
Save shijiezhou1/b8f1b1c5e51df35ce812222507f6aadc to your computer and use it in GitHub Desktop.
Flatten Array Manually
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment