Skip to content

Instantly share code, notes, and snippets.

View ForkInSpace's full-sized avatar
🏠
Working from home

Zaza ForkInSpace

🏠
Working from home
View GitHub Profile
@ForkInSpace
ForkInSpace / arrayFlatten
Created November 17, 2018 20:09
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].
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);