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
function flatten(array) { | |
return array.reduce( (acc, e) => { | |
if(Array.isArray(e)) { | |
// if the element is an array, fall flatten on it again and then take the returned value and concat it. | |
return acc.concat(flatten(e)); | |
} else { | |
// otherwise just concat the value. | |
return acc.concat(e); | |
} | |
}, [] ) // initial value for the accumulator is [] |