flatten using recursionYou are reviewing a pull request that introduces a flatten utility.
Implement flatten(input) such that:
input
is an Array that may contain nested Arrays to arbitrary depth.
Examples:
flatten([1, [2, 3], [[4]], 5]) -> [1, 2, 3, 4, 5]
flatten([]) -> []
flatten([1, [2, [3]]]) -> [1, 2, 3]
// PR version
export function flatten(arr) {
const out = [];
function helper(x) {
for (const i in x) {
const v = x[i];
if (typeof v === 'object') {
helper(v);
} else {
out.push(v);
}
}
}
helper(arr);
return out;
}
null
, sparse arrays, strings).
Login required