Skip to content

Instantly share code, notes, and snippets.

@vitalipe
Created October 11, 2016 01:13
Show Gist options
  • Select an option

  • Save vitalipe/cfa5222ce3e5cc87de184fe678c0cede to your computer and use it in GitHub Desktop.

Select an option

Save vitalipe/cfa5222ce3e5cc87de184fe678c0cede to your computer and use it in GitHub Desktop.
get-in form clojure in JS
// 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));
};
@laurentsenta
Copy link

laurentsenta commented Dec 20, 2018

Dear future me, what about: _.get(obj, path)
https://lodash.com/docs/4.17.11#get

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment