Skip to content

Instantly share code, notes, and snippets.

@zulrang
Created April 14, 2016 01:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zulrang/97581104b40345b5a0a3f4f0a95f3434 to your computer and use it in GitHub Desktop.
Save zulrang/97581104b40345b5a0a3f4f0a95f3434 to your computer and use it in GitHub Desktop.
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