_.merge克隆子文档,而不是更新 [英] _.merge clones sub-documents instead of updating

查看:140
本文介绍了_.merge克隆子文档,而不是更新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的角fullstack 的项目,我尝试更新的项目添加一个新的子文档到它。当合并前检查我的 req.body 这一切看起来理所应当。然而,合并后更新的文件又增加了一个新的子文档,因为它应该,唯一的问题是,这个新的文件是不是一个它应该是一个已经存在的克隆。

_。合并 lodash

在code:

 的console.log('');
    的console.log(合并之前的注意事项:',entry.notes);    的console.log('');
    的console.log('合并前body.notes:',req.body.notes);    VAR更新= _.merge(入门,req.body);    的console.log('');
    的console.log(合并后的注意事项:',updated.notes);


和我的控制台结果运行code时:

合并前

 注:
[
    {
        内容:'< P>测试与LT; / P>,
        日期:星期四2015年4月16日10时14分44秒GMT + 0200(罗,sommertid)
        作者:55193679026666b00554d00e,
        _id:552f6f7435828478156f6103,
        注意事项:[]
    }
]合并前body.notes:
[
    {
        内容:'< P>测试与LT; / P>,
        日期:星期四2015年4月16日10时14分44秒GMT + 0200(罗,sommertid)
        作者:55193679026666b00554d00e,
        _id:552f6f7435828478156f6103,
        注意事项:[]
    },
    {
        内容:'< P>喇嘛喇嘛< / P>,
        日期:2015-04-16T08:25:24.431Z',
        作者:55193679026666b00554d00e
    }
]合并后指出:
[
    {
        内容:'< P>测试与LT; / P>,
        日期:星期四2015年4月16日10时14分44秒GMT + 0200(罗,sommertid)
        作者:55193679026666b00554d00e,
        _id:552f6f7435828478156f6103,
        注意事项:[]
    },
    {
        内容:'< P>测试与LT; / P>,
        日期:星期四2015年4月16日10时14分44秒GMT + 0200(罗,sommertid)
        作者:55193679026666b00554d00e,
        _id:552f6f7435828478156f6103,
        注意事项:[]
    }
]


解决方案

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

  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
  }
}
*/

这篇关于_.merge克隆子文档,而不是更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆