Created
April 14, 2016 01:17
-
-
Save zulrang/97581104b40345b5a0a3f4f0a95f3434 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
function flatten(arr) { | |
var result = []; | |
// iterate through array | |
for(var i = 0; i < arr.length; i++) { | |
// check if number or array | |
if(typeof arr[i] === "object") { | |
// push values of array recursively | |
Array.prototype.push.apply(result, flatten(arr[i])); | |
} else if (typeof arr[i] === "number") { | |
// push number to end of array | |
result.push(arr[i]); | |
} | |
} | |
return result; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment