Created
November 20, 2016 05:27
-
-
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].
This file contains 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() { | |
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