Created
September 19, 2019 07:41
-
-
Save einnar82/6c5e9f18a519b65db701c2417bbbd860 to your computer and use it in GitHub Desktop.
lodash.get behavior using Vanilla JS
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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