Skip to content

Instantly share code, notes, and snippets.

@lawwantsin
Created November 26, 2017 17:42
Show Gist options
  • Save lawwantsin/9e654ea719a2780b66eafee721ab04c3 to your computer and use it in GitHub Desktop.
Save lawwantsin/9e654ea719a2780b66eafee721ab04c3 to your computer and use it in GitHub Desktop.
var flatten = function(integers_array, flatten_array) {
// If this function is called in recursion mode, then we
// need to keep previous recursion results.
var all_results = flatten_array || [];
// We just want to perform any action if there's a
// valid array input and this array contains any value in it.
if (integers_array && integers_array.length > 0) {
integers_array.forEach(function(value) {
if (typeof value === 'number') {
all_results.push(value);
} else if (value instanceof Array) {
flatten(value, all_results);
}
});
}
// At this point, all values were evaluated and we
// have a flat Array of numbers.
return all_results;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment