Open In App

Cloning an Object with Lodash

Last Updated : 23 Jul, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

Cloning an object refers to creating a copy of an existing object. This is particularly useful in JavaScript to avoid unintentional mutations of the original object. Lodash provides various methods for cloning objects, allowing for shallow or deep copies depending on the use case.

Below are the following approaches to clone an object with Lodash:

Shallow Clone using _.assign() Method

The _.assign() method creates a shallow copy of an object by copying its enumerable properties to a target object.

Syntax:

_.assign(target, ...sources)

Example: This  shallowClone contains the same properties as the original object. However, since it’s a shallow copy, the nested object b still references the same object in memory as original.b. Thus, modifying shallowClone.b.c also changes original.b.c.

JavaScript
const _ = require('lodash');

const original = { a: 1, b: { c: 2 } };
const shallowClone = _.assign({}, original);

console.log(shallowClone);
shallowClone.b.c = 3;
console.log(original.b.c);

Output:

{ a: 1, b: { c: 2 } }
3

Deep Clone using _.cloneDeep() Method

The _.cloneDeep() method creates a deep clone of the value, ensuring that all nested objects are also cloned.

Syntax:

_.cloneDeep(value)

Example: In this the deepClone is a completely independent copy of the original object. Changes to nested properties in deepClone do not affect the original, demonstrating that the deep clone successfully duplicated all levels of the object.

JavaScript
const _ = require('lodash');

const original = { a: 1, b: { c: 2 } };
const deepClone = _.cloneDeep(original);

console.log(deepClone);
deepClone.b.c = 3;
console.log(original.b.c); 

Output:

{ a: 1, b: { c: 2 } }
2

Shallow Clone using _.pick() Method

The _.pick() method creates a shallow clone of an object, selecting specific properties to include in the new object.

Syntax:

_.pick(object, [paths])

Example: In this the pickedClone contains only the properties a and c from the original object, omitting property b. This operation remains a shallow copy, meaning that if any of the picked properties were objects, they would still reference the same instances as in the original.

JavaScript
const _ = require('lodash');

const original = { a: 1, b: 2, c: 3 };
const pickedClone = _.pick(original, ['a', 'c']);

console.log(pickedClone);

Output:

{ a: 1, c: 3 }

Deep Clone using _.merge() Method

The _.merge() method merges two or more objects, performing a deep clone of the properties being merged.

Syntax:

_.merge(target, ...sources)

Example: In this the mergedClone combines properties from target and source. The nested object b in target is deep-cloned and combined with the nested object b from source, resulting in a new object that includes both properties c and d.

JavaScript
const _ = require('lodash');

const target = { a: 1, b: { c: 2 } };
const source = { b: { d: 3 } };
const mergedClone = _.merge(target, source);

console.log(mergedClone); 

Output:

{ a: 1, b: { c: 2, d: 3 } }

Similar Reads