Skip to content

Instantly share code, notes, and snippets.

@a-r-d
Created December 7, 2017 01:12
Show Gist options
  • Save a-r-d/c2b4bad1a131cd86ab34aecad03e8267 to your computer and use it in GitHub Desktop.
Save a-r-d/c2b4bad1a131cd86ab34aecad03e8267 to your computer and use it in GitHub Desktop.
traverse objects iteratively
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