Skip to content

Instantly share code, notes, and snippets.

@twmulloy
Created June 7, 2017 16:26
Show Gist options
  • Save twmulloy/99538268c10bcd6e792130d385406eb4 to your computer and use it in GitHub Desktop.
Save twmulloy/99538268c10bcd6e792130d385406eb4 to your computer and use it in GitHub Desktop.
function flatten() {
var result = [];
var currArg;
for(var i = 0; i < arguments.length; i++) {
currArg = arguments[i];
if (currArg.constructor === Array) {
result.push.apply(result, flatten.apply(this, currArg))
} else {
result.push(currArg);
}
}
return result;
}
console.log('output ', flatten([
1, 2, 3, 4, 5, 6, [7, 8, 9, [0, ['a', 'b', 'c']]]
]))
// output [ 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 'a', 'b', 'c' ]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment