Created
November 26, 2017 17:42
-
-
Save lawwantsin/9e654ea719a2780b66eafee721ab04c3 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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