Last active
January 16, 2020 02:29
-
-
Save d13/207840bf7d02dee790b485d9fde577a6 to your computer and use it in GitHub Desktop.
JS util ideas
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 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; | |
} | |
} | |
} | |
} |
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
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