Skip to content

Instantly share code, notes, and snippets.

@AndrejGajdos
Created March 20, 2016 19:34
Show Gist options
  • Save AndrejGajdos/1d56692d6da7cd8a4057 to your computer and use it in GitHub Desktop.
Save AndrejGajdos/1d56692d6da7cd8a4057 to your computer and use it in GitHub Desktop.
Function for flattening nested array
/**
* Flatten nested array
* @param {Array} array Nested array
* @return {Array} Flat array
*/
function flatten(array) {
return array.reduce(function (flat, toFlatten) {
// if current item is array flatten it, otherwise concatenate value to processed flat array
return flat.concat(Array.isArray(toFlatten) ? flatten(toFlatten) : toFlatten);
}, []);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment