Last active
November 16, 2018 16:36
-
-
Save codeepic/ef9de9290da9574f1ea1 to your computer and use it in GitHub Desktop.
Array Flattener in JS
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
var a = [[1,2,[3]],4, [5, [6, 7, [8, [9]]]]]; | |
function flattenNestedArrays(arr){ | |
var flattenedArray = []; | |
(function flattenArray(arr){ | |
arr.forEach(function(item){ | |
if(Array.isArray(item)){ | |
flattenArray(item); | |
}else{ | |
flattenedArray.push(item); | |
} | |
}); | |
})(arr); | |
return flattenedArray; | |
} | |
console.log(flattenNestedArrays(a)); // [1,2,3,4,5,6,7,8,9] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Or simpler: