
8

Lodash's cloneDeep() Function
source link: https://masteringjs.io/tutorials/lodash/clone-deep
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

Lodash's cloneDeep() Function
Jan 3, 2023
You can use lodash's cloneDeep()
function to deep copy a value.
const _ = require('lodash');
const obj = [{ a: 1, b: 2 }];
const copy = _.cloneDeep(obj);
console.log(copy); // [{ a: 1, b: 2 }];
With Classes
If you call cloneDeep()
on an instance of a class, cloneDeep()
will also copy the class information.
class MyClass {};
const obj = new MyClass();
const copy = _.cloneDeep(obj);
copy instanceof MyClass; // true
Warning: Proxies
cloneDeep()
does not work well with Mongoose documents or reactive objects in Vue.
With Vue, the cloned object will not be reactive.
With Mongoose documents, you won't be able to save the cloned document.
const _ = require('lodash');
const mongoose = require('mongoose');
async function run() {
await mongoose.connect('mongodb://localhost:27017/test5');
const arr2Schema = new mongoose.Schema({ _id: false, prop1: String });
const arr1Schema = new mongoose.Schema({ _id: false, arr2: [arr2Schema] });
const Test = mongoose.model(
'Test',
new mongoose.Schema({
arr1: [arr1Schema],
field: String,
}),
);
let doc = new Test({ arr1: [{ arr2: [{ prop1: 'test' }] }] });
await doc.save();
const doc2 = await Test.findById(doc._id);
const newDoc = _.cloneDeep(doc2).set({ field: 'test' });
await newDoc.save(); // Mongoose-internal TypeError
}
run();
In general, we only recommend using cloneDeep()
on POJOs.
More Lodash Tutorials
</div
Recommend
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK