Created
October 11, 2016 01:13
-
-
Save vitalipe/cfa5222ce3e5cc87de184fe678c0cede to your computer and use it in GitHub Desktop.
get-in form clojure in JS
This file contains hidden or 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
| // returns the value in a nested associative structure, | |
| // just like (get-in) in clojure | |
| var getIn = function (obj, path) { | |
| if (!_(path).isArray()) // normalize | |
| path = _(arguments).toArray().rest().value(); | |
| if (path.length === 0) | |
| return obj; | |
| if (!_(obj).has(_.first(path))) | |
| return; | |
| return getIn(obj[_.first(path)], _.rest(path)); | |
| }; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Dear future me, what about:
_.get(obj, path)https://lodash.com/docs/4.17.11#get