Skip to content

Instantly share code, notes, and snippets.

@funkjunky
Created October 24, 2016 20:33
Show Gist options
  • Save funkjunky/e24490511641d9fe4d355be96d387880 to your computer and use it in GitHub Desktop.
Save funkjunky/e24490511641d9fe4d355be96d387880 to your computer and use it in GitHub Desktop.
Flatten's an array any level deep. (for Citrus Byte) [written in ES6 JS]
//Mutable version
const flatten = (arr) =>
arr.reduce((c, v) =>
(Array.isArray(v))
? c.concat(flatten(v))
: [...c, v]
, []);
//or more effecient, in place
const flatten_in = (arr) =>
arr.reduce((c, v) => {
(Array.isArray(v))
? c.push(...flatten(v))
: c.push(v)
return c;
}, []);
console.log(flatten([[1,2,[3]],4]));
export default flatten;
export flatten_in;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment