Created
December 2, 2016 10:17
-
-
Save jinnabaalu/43ce327e86de50be00f0526b4e765c62 to your computer and use it in GitHub Desktop.
Converting the ArbitrarilyNestedArrays into FlatArray
This file contains hidden or 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
<!DOCTYPE html> | |
<html> | |
<body> | |
<p id="demo"></p> | |
<script> | |
var arbitrarilyNestedArray = [[1,2,[3]],4]; | |
var flatArray = []; | |
function convertArbitrarilyNestedArrayToFlatArray(arbitrarilyNestedArray) { | |
for (var i = 0; i < arbitrarilyNestedArray.length; i++) { | |
if (Array.isArray(arbitrarilyNestedArray[i])) { | |
convertArbitrarilyNestedArrayToFlatArray(arbitrarilyNestedArray[i]); | |
} else { | |
flatArray.push(arbitrarilyNestedArray[i]); | |
} | |
} | |
return flatArray; | |
} | |
document.getElementById("demo").innerHTML = convertArbitrarilyNestedArrayToFlatArray(arbitrarilyNestedArray); | |
</script> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment