Last active
September 1, 2016 01:35
-
-
Save NoahDragon/24498e39664416d2d4346c74060668ae to your computer and use it in GitHub Desktop.
Answers for questions from Stackoverflow http://stackoverflow.com/questions/39260085/how-to-get-all-values-out-of-array-and-its-sub-arrays http://stackoverflow.com/questions/27266550/how-to-flatten-nested-array-in-javascript
This file contains hidden or 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
var result = []; | |
function isArray (value) { | |
return value.constructor === Array; | |
} | |
function extract (value) { | |
if (isArray(value)) { | |
value.forEach(function (each) { | |
if (isArray(each)) extract(each); | |
else result.push(each); | |
}); | |
} else result.push(value); | |
return result; | |
} | |
console.log(extract([["a", 2, [3, "mn"], "bu"], "la", [12, 34, "ab"]])); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment