Skip to content

Instantly share code, notes, and snippets.

@Om4ar
Last active March 4, 2018 21:10
Show Gist options
  • Save Om4ar/3db2de3bf978717e92b4e34981bc60c7 to your computer and use it in GitHub Desktop.
Save Om4ar/3db2de3bf978717e92b4e34981bc60c7 to your computer and use it in GitHub Desktop.
// array , property string
// https://medium.com/javascript-inside/safely-accessing-deeply-nested-values-in-javascript-99bf72a0855a
const get = (p, o) =>
p.reduce((xs, x) => (xs && xs[x]) ? xs[x] : null, o)
// let's pass in our props object...
console.log(get(['user', 'posts', 0, 'comments'], props))
// [ 'Good one!', 'Interesting...' ]
console.log(get(['user', 'post', 0, 'comments'], props))
// null
export const isEmpty = value =>
value === null ||
value === undefined ||
value === "undefined" ||
value.length === 0 ||
value === {};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment