Skip to content

Instantly share code, notes, and snippets.

@d13 d13/deep-search.js

Last active Jan 16, 2020
Embed
What would you like to do?
JS util ideas
function keyFirstSearch(obj, term) {
for (const key of Object.keys(obj)) {
if (key === term) {
return obj[term];
}
const prop = obj[key];
if (typeof prop === 'object') {
const nested = keyFirstSearch(prop, term);
if (nested !== undefined) {
return nested;
}
}
}
}
const data = {
foo: {
bar: {
baz: {
BI_STATUS: true
}
}
}
};
console.log(keyFirstSearch(data, 'BI_STATUS'));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.