What is the _.set() method in Lodash?

Overview

The _.set() method in Lodash is used to set the value at a specific path of the object.

Syntax

_.set(object, path, value)

Parameters

This method accepts the following parameters:

  • object: This is the object where the given value needs to be set.
  • path: This is the path at which the given value needs to be set.
  • value: This is the value to be set.

Return value

This method modifies the given object and returns the modified object.

Example

Let’s look at an example of the _.set() method in the code snippet below:

Console

Explanation

In the HTML tab:

  • Line 5: We import the lodash script.

In the JavaScript tab:

  • Lines 2 to 11: We create an object that is to be modified.
  • Lines 14 to 17: We create an object collegeEducation that is the value to be set.
  • Line 20: We create a variable path where the value is set.
  • Line 23: We use the _.set() method and pass the object, path and value as parameters. And we print the output on the console.

Output

The _.set() method sets the given value at the specified path and returns the modified object, which is printed to the console.

Free Resources