Skip to content

Instantly share code, notes, and snippets.

@dbagia
Last active February 4, 2017 12:20
Show Gist options
  • Save dbagia/1e4898e43cfced80768f513e813ea940 to your computer and use it in GitHub Desktop.
Save dbagia/1e4898e43cfced80768f513e813ea940 to your computer and use it in GitHub Desktop.
Flatten array
//flatten:: a -> a -> a
var flatten = function(arr, result){
result = result || [];
arr.map(function(entry){
if(Array.isArray(entry)){
//call flatten recursively since entry is an array
return flatten(entry,result);
} else{
//push to result if entry is not an array
result.push(entry);
}
});
return result;
}
@dbagia
Copy link
Author

dbagia commented Feb 4, 2017

Test the above gist by console.log(flatten([[1,[2],[3]],4,[5,6,[7,[8,9,[10,11]]]]]));

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment