How to create a shallow clone of a value using Lodash
Overview
In Lodash, we can create a shallow clone of a value using the _.clone() method.
Syntax
_.clone(value);
Syntax of _.clone()
Parameters
This method accepts the following parameters:
value: This is the value to be cloned.
Return value
This method returns a shallow clone of the value. If the value is not able to be cloned, this method returns an empty object.
Example
Let’s look at an example of creating a shallow clone of a value using the _.clone() method in the code snippet below.
Explanation
In the HTML tab, we see the following:
- Line 5: We import the
lodashscript.
In the JavaScript tab, we see the following:
- Lines 2–5: We create a variable
valueand initialize it.
- Line 8: We create a shallow clone of
value.
- Line 10: We print the shallow clone on the console.