Last active
August 19, 2017 19:13
-
-
Save denisdbell/aaaaa78b468961e9104563030c4bd721 to your computer and use it in GitHub Desktop.
Code to flatten array
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
var array = [[1,2,[3,4]],4]; | |
function flattenArray(array){ | |
var length = array.length; | |
var flattenedArray = new Array(); | |
for(var x = 0; x < length; x++){ | |
if( Array.isArray(array[x]) && array[x].length > 1){ | |
for (var j = 0; j < array[x].length; ++j){ | |
flattenedArray.push(array[x][j]); | |
} | |
}else{ | |
console.log("not array " + array[x]); | |
flattenedArray.push(array[x]); | |
} | |
} | |
return flattenedArray; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment