Last active
August 29, 2015 14:15
-
-
Save hammeiam/9d07990e9aa6f15458d6 to your computer and use it in GitHub Desktop.
A simple implementation of an in-place flatten function in javascript
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 inPlaceFlatten(arr){ | |
for (var i = 0; i < arr.length; i++) { | |
if(arr[i] instanceof Array){ | |
var temp = inPlaceFlatten(arr[i]); | |
arr = arr.slice(0, i).concat(temp, arr.slice(i + 1)); | |
i += temp.length - 1; | |
} | |
}; | |
return arr; | |
} | |
// console.log(inplaceFlatten([1,[2,3,[4, 5]],6])) -> [1,2,3,4,5,6] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment