Created
July 20, 2016 09:03
-
-
Save optimistanoop/a5051493fe0192da5ce907205e11d45b to your computer and use it in GitHub Desktop.
Array flatter using recursion.
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
function flatter(arr , res){ | |
if(!Array.isArray(arr)){ | |
return; | |
} | |
for(var elem in arr){ | |
if(Array.isArray(arr[elem])){ | |
flatter(arr[elem], res); | |
}else { | |
res.push(arr[elem]); | |
} | |
} | |
return res; | |
} | |
var res = flatter([1,[2,3,[4,5,6,[7,8]],9],10], []); | |
console.log(res); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment