Skip to content

Instantly share code, notes, and snippets.

@ramsunvtech
Created June 26, 2016 01:14
Show Gist options
  • Select an option

  • Save ramsunvtech/91b352de08f4e0896701b04ebf98c648 to your computer and use it in GitHub Desktop.

Select an option

Save ramsunvtech/91b352de08f4e0896701b04ebf98c648 to your computer and use it in GitHub Desktop.
JS Array Deep Iterator
var arrayHelper = function () {
this.myArray = [];
this.deepIterator = function(givenArray, isRecursiveCall) {
var _this = this;
// Iterating the Given Array.
givenArray.forEach(function (value, index) {
// Check if iterated item is an Complex Variable.
if(Array.isArray(value)) {
_this.deepIterator(value, true);
}
else {
_this.myArray.push(value);
}
});
var singleArray = _this.myArray;
// Empty the myArray Property, if its actual call.
if(!isRecursiveCall) {
_this.myArray = [];
}
return singleArray;
}
}
var arrayHelper = new arrayHelper();
var list = [[1,2,[3]],4];
var complexList = [[1], [[2]], [[[3]]], [[[[4]]]], [[[[[5]]]]]];
console.log('Given Array: ', arrayHelper.deepIterator(list));
console.log('Complex Array: ', arrayHelper.deepIterator(complexList));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment