Skip to content

Instantly share code, notes, and snippets.

@nestoralonso
Created January 27, 2021 19:34
Show Gist options
  • Save nestoralonso/0c1ae1a709cdf728dc9153c5377f8df2 to your computer and use it in GitHub Desktop.
Save nestoralonso/0c1ae1a709cdf728dc9153c5377f8df2 to your computer and use it in GitHub Desktop.
Traverse the values of an object
function traverse(obj, callback) {
const stack = [];
stack.push(obj);
while (stack.length) {
const current = stack[0];
for (const key of Object.keys(current)) {
if (current[key] != null && typeof current[key] === 'object') {
stack.push(current[key]);
continue;
}
callback(key, current[key], current);
}
stack.shift();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment