Skip to content

Instantly share code, notes, and snippets.

@robbieferrero
Last active December 4, 2017 19:18
Show Gist options
  • Save robbieferrero/ec2f5e7e500db52ccd8a0e4c2c372b4a to your computer and use it in GitHub Desktop.
Save robbieferrero/ec2f5e7e500db52ccd8a0e4c2c372b4a to your computer and use it in GitHub Desktop.
Deep Flatten
/**
* A utility function to flatten an array, nested to any depth.
* @param {Array} initialArray
*
*/
function deepFlatten(initialArray) {
return initialArray.reduce((acc, v) => {
return acc.concat(Array.isArray(v) ? deepFlatten(v) : v);
}, []);
}
/**
* Example usage
*/
// Example array
const test = [[1, 2, [3]], 4];
console.log(deepFlatten(test));
// [1,2,3,4]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment