创建两个对象数组的差异模式并将其应用于 Javascript 中的另一个数组

Create pattern of difference of two arrays of objects and apply it to another array in Javascript

我有两个不同对象的数组(可以包含额外的对象数组):

var array1 = [{"x":10,"y":10},{"a":10,"b":10,"c":10}]

和另一个数组:

var array2 = [{"x":10,"y":10},{"a":10,"b":25,"c":10}]

我需要"what is different on second array":

var tmp = [{},{"b":25}] e.g.

现在用模式 tmp

更新 array1

有人可以帮我解决这个问题吗?非常感谢您的宝贵时间。

解决方案:

https://github.com/flitbit/diff :)

let compareJSON = (a, b) => {
  let ret = {};

  for (let i in b) {
    if (!a.hasOwnProperty(i) || b[i] !== a[i]) {
      ret[i] = b[i];
    }
  }
  return ret;
};

let a = [{"x":10,"y":10},{"a":10,"b":10,"c":10}]
let b = [{"x":10,"y":10},{"a":10,"b":25,"c":10}]

let length = a.length > b.length ? a.length : b.length;
for (let i = 0; i < length; i++) {
  console.log(compareJSON(a[i], b[i]));
}