Skip to content

Instantly share code, notes, and snippets.

@rajkumarpb
Created December 6, 2016 10:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rajkumarpb/c21a7970dca88ae3afd6283b2ca8950d to your computer and use it in GitHub Desktop.
Save rajkumarpb/c21a7970dca88ae3afd6283b2ca8950d to your computer and use it in GitHub Desktop.
Flat an array of arrays
var inArr = [[[1, 2],],[3],[[4, 5],[6]]];
var result = [];
function flattenArray(arr) {
for (var i = 0; i < arr.length; i++) {
if (Array.isArray(arr[i]))
flattenArray(arr[i]);
else
result.push(arr[i]);
}
return result;
}
alert(flattenArray(inArr));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment