Syntax, Parameter and Return Value
Syntax:
_.clone(value)
Parameters:
value (Object|Array): The value to clone.
Return Value:
Returns the shallow cloned value.
Examples
Cloning an Array:
JavaScript
var _ = require('lodash');
var array = [1, 2, { a: 3 }];
var clonedArray = _.clone(array);
clonedArray[2].a = 4;
console.log(array);
console.log(clonedArray);
You can also try this code with Online Javascript Compiler
Run Code
Output:
[1, 2, { a: 4 }]
[1, 2, { a: 4 }]
Demonstrates shallow cloning of an array. Note that changes to nested objects affect both the original and the clone.
Cloning an Object:
JavaScript
var object = { a: 1, b: { c: 2 } };
var clonedObject = _.clone(object);
clonedObject.b.c = 3;
console.log(object);
console.log(clonedObject);
You can also try this code with Online Javascript Compiler
Run Code
Output:
{ a: 1, b: { c: 3 } }
{ a: 1, b: { c: 3 } }
Shows shallow cloning of an object. Changes to nested objects reflect in both the original and the clone.
Preventing Modification of Original Array:
JavaScript
var numbers = [1, 2, 3];
var clonedNumbers = _.clone(numbers);
clonedNumbers.push(4);
console.log(numbers);
console.log(clonedNumbers);
You can also try this code with Online Javascript Compiler
Run Code
Output:
[1, 2, 3]
[1, 2, 3, 4]
An example of cloning an array to modify the clone without affecting the original.
Cloning with Primitive Values:
JavaScript
var value = 'Hello';
var clonedValue = _.clone(value);
console.log(clonedValue);
You can also try this code with Online Javascript Compiler
Run Code
Output:
'Hello'
Demonstrates that cloning primitive values (like strings) simply returns the same value.
Frequently Asked Questions
What's the difference between shallow and deep cloning?
Shallow cloning copies the top-level elements, while nested objects or arrays still refer to the same memory location. Deep cloning, on the other hand, creates a completely independent copy at all levels.
When should you use _.clone() over _.cloneDeep()?
Use _.clone() for simple data structures without deep nesting or when you want nested objects to remain linked to the original. Use _.cloneDeep() for complex structures where you need a fully independent copy.
Does _.clone() work with all types of data?
_.clone() works with most types of data, including objects, arrays, and primitives. However, it does not handle functions or certain complex objects like DOM nodes or special JavaScript objects (like Date, RegExp).
Conclusion
Lodash's _.clone() method is an efficient way to create shallow copies of objects or arrays, ensuring that modifications to the copy do not affect the original data. It is ideal for handling simple data structures where deep cloning is not required.
You can refer to our guided paths on the Coding Ninjas. You can check our course to learn more about DSA, DBMS, Competitive Programming, Python, Java, JavaScript, etc.
Also, check out some of the Guided Paths on topics such as Data Structure and Algorithms, Competitive Programming, Operating Systems, Computer Networks, DBMS, System Design, etc., as well as some Contests, Test Series, and Interview Experiences curated by top Industry Experts.