Skip to content

Instantly share code, notes, and snippets.

View gabriel-yuca's full-sized avatar

Gabriel Saraiva gabriel-yuca

View GitHub Profile
@praveenpuglia
praveenpuglia / javascript-deep-flatten.js
Created July 27, 2018 14:37
Deep flatten a JavaScript array using Array.prototype.reduce
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 []