Skip to content

Instantly share code, notes, and snippets.

@vshjxyz
Created March 15, 2016 11:45
Show Gist options
  • Save vshjxyz/d37bd79717afef63856f to your computer and use it in GitHub Desktop.
Save vshjxyz/d37bd79717afef63856f to your computer and use it in GitHub Desktop.
const input = [[1,2,[3]],4];
const output = [1,2,3,4];
function *flatten(arr) {
for (value of arr) {
if (Array.isArray(value)) {
yield *flatten(value);
} else {
yield value;
}
}
}
console.log(JSON.stringify([...flatten(input)]) == JSON.stringify(output));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment