Created
January 27, 2021 19:34
-
-
Save nestoralonso/0c1ae1a709cdf728dc9153c5377f8df2 to your computer and use it in GitHub Desktop.
Traverse the values of an object
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
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