Skip to content

Instantly share code, notes, and snippets.

@dlebedynskyi
Created April 13, 2017 13:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dlebedynskyi/d6828f9cb4882636077e8eb8fff6c0d5 to your computer and use it in GitHub Desktop.
Save dlebedynskyi/d6828f9cb4882636077e8eb8fff6c0d5 to your computer and use it in GitHub Desktop.
get deep nested object prop. functional way.
// https://medium.com/javascript-inside/safely-accessing-deeply-nested-values-in-javascript-99bf72a0855a
const idx = p => o =>
p.reduce((xs, x) =>
(xs && xs[x]) ? xs[x] : null, o)
const getUserComments = idx(['posts', 0, 'comments'])
const user = {}
console.log(getUserComments(user))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment