Created
March 15, 2016 11:45
-
-
Save vshjxyz/d37bd79717afef63856f to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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