Created
December 7, 2017 01:12
-
-
Save a-r-d/c2b4bad1a131cd86ab34aecad03e8267 to your computer and use it in GitHub Desktop.
traverse objects iteratively
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 traverse(obj) { | |
var stack = []; | |
var i = 0; | |
stack.push(obj); | |
while (stack.length) { | |
for (var j in stack[0]) { | |
i++; | |
if (typeof stack[0][j] === 'object') { | |
stack.push(stack[0][j]); | |
} else { | |
if(String(j) === 'initializeVariantDropdowns' || i % 100000 === 0) { | |
console.log('%s: %s', j, stack[0][j]); | |
} | |
} | |
} | |
if( i > 1000000) { | |
console.log('greater than a million'); | |
break; | |
} | |
stack.shift(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment