_.merge Clones Sub-documents Instead Of Updating
Solution 1:
Try using _.extend
or _.assign
instead:
var updated = _.assign(entry, req.body);
This answer by ShitalShah highlights the differences between merge and extend that is causing duplicates in your resulting object with merge but essentially:
Here's how extend/assign works: For each property in source, copy its value as-is to destination. if property values themselves are objects, there is no recursive traversal of their properties. Entire object would be taken from source and set in to destination.
Here's how merge works: For each property in source, check if that property is object itself. If it is then go down recursively and try to map child object properties from source to destination. So essentially we merge object hierarchy from source to destination. While for extend/assign, it's simple one level copy of properties from source to destination.
JSBin to illustrate the differences:
vardest= {
p: { x:10, y:20},
};varsrc= {
p: { x:20, z:30},
};console.log(_.merge(dest,src));/*
[objectObject] {
p: [objectObject] {
x:20,
y:20,
z:30
}
}
*/console.log(_.extend(dest,src));/*
[objectObject] {
p: [objectObject] {
x:20,
z:30
}
}
*/
Post a Comment for "_.merge Clones Sub-documents Instead Of Updating"