Skip to content

Instantly share code, notes, and snippets.

🎯
Focusing

Zaza ForkInSpace

🎯
Focusing
View GitHub Profile
@ForkInSpace
ForkInSpace / arrayFlatten
Created Nov 17, 2018
Write some code, that will flatten an array of arbitrarily nested arrays of integers into a flat array of integers. e.g. [[1,2,[3]],4] -> [1,2,3,4].
View arrayFlatten
flatten = (array, result) => {
if(array.length === 0) return result;
let head = array[0];
let rest = array.slice(1);
if (Array.isArray(head)) {
return flatten(head.concat(rest), result);
};
result.push(head);
return flatten(rest, result);
You can’t perform that action at this time.