且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

_.merge克隆子文档,而不是更新

更新时间:2023-11-26 22:45:16

尝试使用 _延长 _分配 来代替:

  VAR更新= _.assign(入门,req.body);

回答 按ShitalShah凸显合并之间的差异和扩展,是造成重复你的与合并但本质上生成的目标:


  

下面是如何延长/分配的工作:对于源每个属性,复制其
  值,是到目的地。如果属性值本身也是对象,
  有它们的属性没有递归遍历。整个对象
  将来自源获取并设置为目的地。


  
  

下面是如何合并的工作原理:对于源中的每个属性,检查是否是
  属性是对象本身。如果是然后再往递归和尝试
  到子对象的属性映射从源到目的地。所以
  本质上讲,我们合并对象层次从源到目的地。
  虽然延长/分配,这是一个从属性的简单的复制水平
  源到目的地。


块引用>

JSBin 以说明差异:

  VAR DEST = {
  电话号码:{X:10 Y:20},
};变种SRC = {
  电话号码:{X:20,Z:30},
};的console.log(_合并(DEST,SRC));
/ *
[对象的对象] {
  号码:[对象的对象] {
    X:20,
    Y:20,
    Z:30
  }
}
* /的console.log(_延伸(DEST,SRC));
/ *
[对象的对象] {
  号码:[对象的对象] {
    X:20,
    Z:30
  }
}
* /

In my Angular fullstack project I try to update a project adding a new sub-document to it. When checking my req.body before merging it all looks as it should. However after the merge the updated document has added a new sub-document as it should, the only problem is that this new document is a clone of an already existing one, instead of the one it should have been.

the _.merge is from lodash

the code:

    console.log('');
    console.log('notes before merging : ', entry.notes);

    console.log('');
    console.log('body.notes before merging : ', req.body.notes);

    var updated = _.merge(entry, req.body);

    console.log('');
    console.log('notes after merging : ', updated.notes); 


And my console result when running the code:

notes before merging :  
[
    { 
        content: '<p>testing</p>',
        date: Thu Apr 16 2015 10:14:44 GMT+0200 (Rom, sommertid),
        writer: 55193679026666b00554d00e,
        _id: 552f6f7435828478156f6103,
        notes: [] 
    }
]

body.notes before merging :  
[ 
    { 
        content: '<p>testing</p>',
        date: Thu Apr 16 2015 10:14:44 GMT+0200 (Rom, sommertid),
        writer: 55193679026666b00554d00e,
        _id: 552f6f7435828478156f6103,
        notes: [] 
    },
    { 
        content: '<p>bla bla</p>',
        date: '2015-04-16T08:25:24.431Z',
        writer: 55193679026666b00554d00e
    }
]

notes after merging :  
[
    { 
        content: '<p>testing</p>',
        date: Thu Apr 16 2015 10:14:44 GMT+0200 (Rom, sommertid),
        writer: 55193679026666b00554d00e,
        _id: 552f6f7435828478156f6103,
        notes: [] 
    },
    { 
        content: '<p>testing</p>',
        date: Thu Apr 16 2015 10:14:44 GMT+0200 (Rom, sommertid),
        writer: 55193679026666b00554d00e,
        _id: 552f6f7435828478156f6103,
        notes: [] 
    }
]

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:

var dest = {
  p: { x: 10, y: 20},
};

var src = {
  p: { x: 20, z: 30},
};

console.log(_.merge(dest, src)); 
/*
[object Object] {
  p: [object Object] {
    x: 20,
    y: 20,
    z: 30
  }
}
*/

console.log(_.extend(dest, src));
/*
[object Object] {
  p: [object Object] {
    x: 20,
    z: 30
  }
}
*/