Skip to content

Instantly share code, notes, and snippets.

@ravi0402
Last active September 9, 2019 20:30
Show Gist options
  • Save ravi0402/d18b67103c3c76a4cad9f3d23b0e93fb to your computer and use it in GitHub Desktop.
Save ravi0402/d18b67103c3c76a4cad9f3d23b0e93fb to your computer and use it in GitHub Desktop.
Flatten an array of arbitrarily nested arrays of integers into a flat array of integers
const arrayFlatten = (arr) => {
if (!Array.isArray(arr))return 'The input provided is not an array';
return arr.reduce((x, y) => {
if (Array.isArray(y)) return x.concat(arrayFlatten(y));
return x.concat(y)
}, [])
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment