Skip to content

Instantly share code, notes, and snippets.

@einnar82
Created September 19, 2019 07:41
Show Gist options
  • Save einnar82/6c5e9f18a519b65db701c2417bbbd860 to your computer and use it in GitHub Desktop.
Save einnar82/6c5e9f18a519b65db701c2417bbbd860 to your computer and use it in GitHub Desktop.
lodash.get behavior using Vanilla JS
/**
* Gets the value at `path` of `object`.
* @param {Object} object
* @param {string|Array} path
* @returns {*} value if exists else undefined
*/
const _.get = (object, path) => {
if (typeof path === "string") path = path.split(".").filter(key => key.length);
return path.reduce((dive, key) => dive && dive[key], object);
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment