Skip to content

Instantly share code, notes, and snippets.

@d13
Last active January 16, 2020 02:29
Show Gist options
  • Save d13/207840bf7d02dee790b485d9fde577a6 to your computer and use it in GitHub Desktop.
Save d13/207840bf7d02dee790b485d9fde577a6 to your computer and use it in GitHub Desktop.
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