Skip to content

Instantly share code, notes, and snippets.

@ldmarz
Created August 10, 2018 13:56
Show Gist options
  • Save ldmarz/17511adf1a1b89edb4fca5426c4e69af to your computer and use it in GitHub Desktop.
Save ldmarz/17511adf1a1b89edb4fca5426c4e69af to your computer and use it in GitHub Desktop.
Set and Get lodash example
var someNiceObject = { hello: { someDeepKey: "hello" } };
_.set(someNiceObject, "hello.items[0]", "An item");
// here items was added safely
// someNiceObject === { hello: { someDeepKey: "hello", items: ["An item"] } }
//Here we can access to a not existing key getting a default value instead
var name = _.get(someNiceObject, "someNotExistingKey", "Ldmarz");
// name => Ldmarz
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment