Last active
April 4, 2019 16:01
-
-
Save conor909/4f969718220e03555b2142701dd3b9de to your computer and use it in GitHub Desktop.
Dev test. A browser / production ready implementation of a flatten array function.
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 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