Skip to content

Instantly share code, notes, and snippets.

@imbhargav5
Created January 19, 2017 08:52
Show Gist options
  • Save imbhargav5/98f9b9747bd1a40a1827a73bd31a97d9 to your computer and use it in GitHub Desktop.
Save imbhargav5/98f9b9747bd1a40a1827a73bd31a97d9 to your computer and use it in GitHub Desktop.
const __flattenReducer = (result,value,valueIndex,arr)=>{
if(value instanceof Array){
return value.reduce(__flattenReducer,result);
}else{
result.push(value);
return result;
}
};
const flatten = function(arr){
if(arr instanceof Array){
return Array.prototype.reduce.apply(arr,[__flattenReducer,[]]);
}else{
throw new TypeError('Expected an array');
}
}
module.exports = flatten;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment