Skip to content

Instantly share code, notes, and snippets.

@bjdixon
Created May 9, 2017 05:26
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 bjdixon/ddaa821b189ce61631f258e82a7d0641 to your computer and use it in GitHub Desktop.
Save bjdixon/ddaa821b189ce61631f258e82a7d0641 to your computer and use it in GitHub Desktop.
Use a path to get a nested value in an object. eg. obj.user.address.city == path(['user', 'address', 'city'], obj)
const isObject = item => item && typeof item === 'object' && !Array.isArray(item);
const path = ([...paths], obj) => {
const tmp = obj[paths.shift()];
return !isObject(tmp) || !paths.length ? tmp : path(paths, tmp);
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment