Created
June 26, 2016 01:14
-
-
Save ramsunvtech/91b352de08f4e0896701b04ebf98c648 to your computer and use it in GitHub Desktop.
JS Array Deep Iterator
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
| 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