Skip to content

Instantly share code, notes, and snippets.

@upalexgill
Created March 12, 2024 09:16
Show Gist options
  • Select an option

  • Save upalexgill/c520e33d5da5650be5c1bb87a985a96d to your computer and use it in GitHub Desktop.

Select an option

Save upalexgill/c520e33d5da5650be5c1bb87a985a96d to your computer and use it in GitHub Desktop.
Find property by key in nested object
function findByKey(obj, keyToFind) {
return Object.entries(obj)
.reduce((acc, [key, value]) => (key === keyToFind)
? acc.concat(value)
: (typeof value === 'object')
? acc.concat(findByKey(value, keyToFind))
: acc
, [])
}
// Usage.
findByKey(obj, key)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment