Skip to content

Instantly share code, notes, and snippets.

@hammeiam
Last active August 29, 2015 14:15
Show Gist options
  • Save hammeiam/9d07990e9aa6f15458d6 to your computer and use it in GitHub Desktop.
Save hammeiam/9d07990e9aa6f15458d6 to your computer and use it in GitHub Desktop.
A simple implementation of an in-place flatten function in javascript
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