Skip to content

Instantly share code, notes, and snippets.

@conor909
Last active April 4, 2019 16:01
Show Gist options
  • Save conor909/4f969718220e03555b2142701dd3b9de to your computer and use it in GitHub Desktop.
Save conor909/4f969718220e03555b2142701dd3b9de to your computer and use it in GitHub Desktop.
Dev test. A browser / production ready implementation of a flatten array function.
function flattenArray(array) {
var flattened = [];
function loop(array) {
for (var i=0; i < array.length; i++) {
var value = array[i];
if (typeof value === 'object' && value.constructor === Array) {
loop(value);
} else {
flattened.push(value);
}
}
}
loop(array);
return flattened;
}
var myArrayOfArrays = [ [ 1, 2, 3 ], [ 12, 13, 14, [ 90, 91, 92] ] ];
flattenArray(myArrayOfArrays); // [1, 2, 3, 12, 13, 14, 90, 91, 92]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment