Skip to content

Instantly share code, notes, and snippets.

@JoviDeCroock
Created June 2, 2019 11:28
Show Gist options
  • Save JoviDeCroock/29dd0f148330602d9a93d679612b5067 to your computer and use it in GitHub Desktop.
Save JoviDeCroock/29dd0f148330602d9a93d679612b5067 to your computer and use it in GitHub Desktop.
const inputArray = [[1, 2, [3]], 4];
const flatten = input => {
return input.reduce((acc, x) => {
if (Array.isArray(x)) {
return [...acc, ...flatten(x)];
} else {
return [...acc, x];
}
}, []);
};
console.log(flatten(inputArray)); // [1, 2, 3, 4]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment