Skip to content

Instantly share code, notes, and snippets.

@batogov
Created September 23, 2017 10:58
Show Gist options
  • Save batogov/67462330bafe6f4999aaf24523e269db to your computer and use it in GitHub Desktop.
Save batogov/67462330bafe6f4999aaf24523e269db to your computer and use it in GitHub Desktop.
Make Array Flat Again!
var result = [];
function flatten(val) {
if (Array.isArray(val)) {
for (var i = 0; i < val.length; i++) {
flatten(val[i]);
}
} else {
result.push(val);
}
}
var arr = [1, 2, [3, 4, [5, 6, 7]]];
flatten(arr);
console.log(result);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment