Created
February 15, 2017 07:16
-
-
Save shettayyy/f52ed1bf785ed4449070fe62201e29f3 to your computer and use it in GitHub Desktop.
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
/** | |
* Flatten an array | |
* E.g [[1,2,[3]],4] -> [1,2,3,4] | |
* | |
* @Contributor Rahul Shetty <[email protected]> | |
* @created Wed Feb 15 12:40:37 2017 | |
*/ | |
function flattenNestedArray(arr) { | |
var result = []; | |
for(var i = 0; i < arr.length; i++) { | |
if(Array.isArray(arr[i])) { | |
result = result.concat(flattenNestedArray(arr[i])); | |
} else { | |
result.push(arr[i]); | |
} | |
} | |
return result; | |
} | |
var flattenedArray = flattenNestedArray([[1,2,[3]],4]); | |
console.log (flattenedArray); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment