Last active
February 4, 2017 12:20
-
-
Save dbagia/1e4898e43cfced80768f513e813ea940 to your computer and use it in GitHub Desktop.
Flatten array
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//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; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Test the above gist by console.log(flatten([[1,[2],[3]],4,[5,6,[7,[8,9,[10,11]]]]]));