DEV Community

Daniel
Daniel

Posted on

The set and get methods from lodash

What is lodash?

  • Lodash simplifies our work by providing lots of methods to handle arrays and objects.

_.get()

  • The get method gets the value from the path that’s been provided as an argument. The first argument is the object, the second is the path of the object and the third is the optional argument discussed which returns the default value
  • So, What if the path doesn’t exist or the path returns ‘undefined’. No worries Lodash allows us to pass an optional third argument which is the default value. In those situations, the default value is returned.

_.set()

  • The set method as the name suggests sets the value to the path of the object. There are three arguments. The first is the object itself, second is the path and the third one is the value.
  • So, What if the path doesn’t exist. The set() automatically creates a path for us. Here the ‘c’ path doesn’t exist but its automatically created.

Discussion (0)