Skip to content

Instantly share code, notes, and snippets.

@joeSaad
Created August 8, 2017 16:36
Show Gist options
  • Save joeSaad/9cca4e0468f1903c15de8466877a62b4 to your computer and use it in GitHub Desktop.
Save joeSaad/9cca4e0468f1903c15de8466877a62b4 to your computer and use it in GitHub Desktop.
Flatten an array
function flattenMyArr(nArr) {
for (let i = 0; i < nArr.length; i++) {
if (Array.isArray(nArr[i])) {
flattenMyArr(nArr[i])
} else {
flatArr.push(nArr[i])
}
}
return flatArr;
}
var flatArr = [];
var p = [[1, 2, [3]], 4];
var q = flattenMyArr(p);
console.log(q);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment