Created
January 19, 2017 08:52
-
-
Save imbhargav5/98f9b9747bd1a40a1827a73bd31a97d9 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
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