Skip to content

Instantly share code, notes, and snippets.

@herbertpimentel
Last active October 3, 2018 19:31
Show Gist options
  • Save herbertpimentel/9c3716b6c767fb7b4f296a889a46cefa to your computer and use it in GitHub Desktop.
Save herbertpimentel/9c3716b6c767fb7b4f296a889a46cefa to your computer and use it in GitHub Desktop.
/**
* @description Flats an array with any nested level deep. e.g. [[1,2,[3]],4] -> [1,2,3,4]
*/
export const flatArray = (arr) => {
return (arr || []).reduce((previous, current) =>
// call it recursivelly, adds support to nested arrays any level deep
previous.concat(Array.isArray(current) ? flatArray(current) : current)
, []);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment