使用Javascript对象数组,我希望编辑某些值,然后获取更改后的对象数组。
a=[{x:1,y:2},{x:2,y:4}];
b = _.clone(a);
// When I change b, a is also changed. I can no longer know the original a.
////////////////////////////////
a=[{x:1,y:2},{x:2,y:4}];
b = _.map( a, _.clone );
// When I change b, a is not changed,
// but I cannot find only the changed JSONs.
// Every JSON between a and b are considered different.
如何实现以下效果?
a=[{x:1,y:2},{x:2,y:4}];
b = SOME_CLONE_OF_a;
b[0].x=5;
DIFF(b,a) // want to get [{x:5,y:2}]
DIFF(a,b) // want to get [{x:1,y:2}]
[编辑]
这个问题并不是如何使用underscore克隆对象数组?的重复。
那里提供的答案回答了那个问题(如何克隆),但并没有回答我的问题(如何克隆并知道差异)。
这个问题中的DEMO使用了那个答案中的技巧,并演示了它无法找到想要的差异。