Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
function traverseLevelOrder(thing, predicate, maxDepth=20) {
function traverse(thing, predicate, targetDepth, depth=0) {
if (targetDepth !== depth) {
for (const key in thing) {
const val = traverse(thing[key], predicate, targetDepth, depth + 1);
if (val) {
return val;
}
}
return;
}
for (const key in thing) {
if (predicate(thing[key])) {
return thing[key];
}
}
}
for (let i = 0; i < maxDepth; i++) {
const val = traverse(thing, predicate, i);
if (val) {
return val;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment