Skip to content

Instantly share code, notes, and snippets.

@jinnabaalu
Created December 2, 2016 10:17
Show Gist options
  • Save jinnabaalu/43ce327e86de50be00f0526b4e765c62 to your computer and use it in GitHub Desktop.
Save jinnabaalu/43ce327e86de50be00f0526b4e765c62 to your computer and use it in GitHub Desktop.
Converting the ArbitrarilyNestedArrays into FlatArray
<!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