Created
December 29, 2016 14:56
-
-
Save kunjee17/8eaa2a28dc7a8487f6b8bb60ca8ff558 to your computer and use it in GitHub Desktop.
Javascript flatten array.
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 test = [[1,2,[3]],4]; | |
var result = []; | |
//[1,2,3,4] | |
var process = function(arr) { | |
arr.forEach(function(a){ | |
if(Array.isArray(a)){ | |
process(a); | |
}else { | |
result = result.concat(a); | |
} | |
}); | |
} | |
process(test); | |
console.log(result); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment