我的jsbin链接在这里: http://jsbin.com/siyux/1/edit
以下是我的代码:
var dest = {
owner: '53a96efe07b7570013bdcb8b',
schedule: 'Thu Jul 03 2014 20:00:00 GMT+0530 (India Standard Time)',
_id: '53b55f52dafac9f40984fe30',
__v: 0,
a: [],
result:{
markCard: []
},
parent: {},
b: [],
c: [],
d: { teamRef: '53ad727d62290bc8178695fa', squad: [] },
e: { teamRef: '53ad726e62290bc8178695f9', squad: [] }
};
var src = {
markCard:
[ { i: 1,
bt: [Object],
bl: [Object],
ts: [Object],
sc: [Object] },
{ i: 2,
bt: [Object],
bl: [Object],
ts: [Object],
sc: [Object] } ],
srcA:
{ __v: 0,
_id: '53ad726e62290bc8178695f9',
name: 'T-H',
owner: '53a96efe07b7570013bdcb8b' },
srcB: { __v: 0,
_id: '53ad726e62290bc8178695f0',
name: 'T-M',
owner: '53a96efe07b7570013bdcb8b' },
srcC: false,
srcD: 'Bt'
};
//console.log(_.merge(dest, src));
dest.result=_.extend(dest.result, src);
console.log(dest);
预期输出:
dest = {
owner: '53a96efe07b7570013bdcb8b',
schedule: 'Thu Jul 03 2014 20:00:00 GMT+0530 (India Standard Time)',
_id: '53b55f52dafac9f40984fe30',
__v: 0,
a: [],
result:{
markCard:
[ { i: 1,
bt: [Object],
bl: [Object],
ts: [Object],
sc: [Object] },
{ i: 2,
bt: [Object],
bl: [Object],
ts: [Object],
sc: [Object] } ],
srcA:
{ __v: 0,
_id: '53ad726e62290bc8178695f9',
name: 'T-H',
owner: '53a96efe07b7570013bdcb8b' },
srcB: { __v: 0,
_id: '53ad726e62290bc8178695f0',
name: 'T-M',
owner: '53a96efe07b7570013bdcb8b' },
srcC: false,
srcD: 'Bt'
},
parent: {},
b: [],
c: [],
d: { teamRef: '53ad727d62290bc8178695fa', squad: [] },
e: { teamRef: '53ad726e62290bc8178695f9', squad: [] }
};
实际输出
dest = {
owner: '53a96efe07b7570013bdcb8b',
schedule: 'Thu Jul 03 2014 20:00:00 GMT+0530 (India Standard Time)',
_id: '53b55f52dafac9f40984fe30',
__v: 0,
a: [],
result:{
markCard:
[ { i: 1,
bt: [Object],
bl: [Object],
ts: [Object],
sc: [Object] },
{ i: 2,
bt: [Object],
bl: [Object],
ts: [Object],
sc: [Object] } ],
srcA:
{ },
srcB: { }
},
parent: {},
b: [],
c: [],
d: { teamRef: '53ad727d62290bc8178695fa', squad: [] },
e: { teamRef: '53ad726e62290bc8178695f9', squad: [] }
};
由于扩展,我无法将srcA和srcB的值扩展到目标。我想只有结构被复制了。有人请告诉我错误的地方。感谢。
答案 0 :(得分:12)
如果您使用lodash使用Mongoose模型,那么您需要先分配数据然后再分配它。
例如:
var data = _.merge(dest.toJSON(), src);
_.extend(dest, data);
dest.save();
这可能是因为Mongoose模型将附加的非枚举/配置附加到导致扩展/合并期间出现问题的对象。