Skip to content

Instantly share code, notes, and snippets.

@rebase-master
Created November 20, 2016 05:27
Show Gist options
  • Save rebase-master/8e09dc2b88bee63a4819e3d990403ad8 to your computer and use it in GitHub Desktop.
Save rebase-master/8e09dc2b88bee63a4819e3d990403ad8 to your computer and use it in GitHub Desktop.
Write some code, that will flatten an array of arbitrarily nested arrays of integers into a flat array of integers. e.g. [[1,2,[3]],4] -> [1,2,3,4].
function flatten() {
var flat = [];
for (var i = 0; i < arguments.length; i++) {
if (arguments[i] instanceof Array) {
flat.push.apply(flat, flatten.apply(this, arguments[i]));
} else {
flat.push(arguments[i]);
}
}
return flat;
}
console.log(flatten(1,[2,3,[4,5,6],4,5,3]))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment