Skip to content

Instantly share code, notes, and snippets.

@vikikamath
Last active December 3, 2016 18:00
Show Gist options
  • Save vikikamath/3773818644a3fb7aa0a80af1f0cf8436 to your computer and use it in GitHub Desktop.
Save vikikamath/3773818644a3fb7aa0a80af1f0cf8436 to your computer and use it in GitHub Desktop.
Flatten deeply nested Array of Integers
/*
@input: Input array to be flattened
@result: Empty Array
@return:
invalid input: Empty Array
valid input (an Array): Flattened Array
*/
function flatten(input, result) {
if (Number.isInteger(input)) result.push(input);
if (Array.isArray(input)) {
input.forEach(function(element) {
flatten(element, result)
})
}
return result;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment