Javatpoint Logo
Javatpoint Logo

Lodash_.get() Method

What is _.get() method?

This function is used to get the value from the path of the object. If the value at the given path is undefined, then it will return the default value.

Syntax:

This function accepts three parameters which are as follows:

  • Object: This is the object with a path, and we get the value from that path.
  • specificPath: This parameter will be of type array or string. This will represent the property or attribute we want to get using the _.get() function.
  • defaultValue: This is the default value of the _.get() function. If the function returns the undefined, then this value will be returned.

Return value: This function will return the value present at the specified path or the default value.

We will see some examples related to the _.get() function:

Example 1:

JavaScript Code:

Output:

69

Explanation:

In the above code, we have imported the lodash library to use the _.get() function. Now, we created the object which contains an array of nested objects. So, as we know, we can use the dot(.) operator. Then we used the _.get() function on the object, and we provided the value we wanted to get using the dot(.) operator in a string. So, we got the result as 69, and we printed it using the console.log() statement.

Example 2:

JavaScript Code:

Output:

69

Explanation:

In the above code, we have imported the lodash library to use the _.get() function. Now, we created the object which contains an array of nested objects. So as we know, we can use the dot(.) operator. Then we used the _.get() function on the object, and we provided the path in the form of an array of strings instead of a single string. Now each element of the string array will represent the values or the element that we accessed by the dot(.) operator. So, we got the result as 69, and we printed it using the console.log() statement.

Example 3:

JavaScript Code:

Output:

defaultValue

Explanation:

In the above code, we have imported the lodash library to use the _.get() function. Now, we have created the object which contains an array of nested objects. So, as we know, we can use the dot(.) operator. Then we used the _.get() function on the object, and we provided the path in the form of an array of strings instead of a single string. Now each element of the string array will represent the values or the element that we accessed by the dot(.) operator. But we mentioned the wrong path so that it will resolve the value as undefined. Now we have set a default value so the result will be the default value, and we will print it using the console.log() statement.

Example 4:

JavaScript Code:

Output:

23

Explanation:

In the above code, we have imported the lodash library to use the _.get() function. Now, we have created the object which contains an array of nested objects. So, as we know, we can use the dot(.) operator. Then we used the _.get() function on the object, and we provided the path in the form of an array of strings instead of a single string. Now, each element of the string array will represent the values or the element that we accessed by the dot(.) operator. But we mentioned the wrong path so that it will resolve the value as undefined. We can put the default value as whatever data type we require. So, we set the default value as 23. Since 'Ankit' does not exist in the array, it will resolve its age from the default value, and the result will be 23, which we will print using the console.log() statement.







Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA