Skip to content

Instantly share code, notes, and snippets.

@waqasajaz
Created March 29, 2017 07:41
Show Gist options
  • Save waqasajaz/838d4facfd196eba101d42ba4a2839c0 to your computer and use it in GitHub Desktop.
Save waqasajaz/838d4facfd196eba101d42ba4a2839c0 to your computer and use it in GitHub Desktop.
Flat the nested array
function flatter(data) {
var result = [];
for (var i = 0; i < data.length; i++) {
if(Array.isArray(data[i])) {
result = result.concat(flatten(data[i]));
} else {
result.push(data[i]);
}
}
return result;
}
console.log(flatter([[1,2,[1,2,3]],4]));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment