如何找到两个JavaScript对象数组之间的差异?

时间:2022-04-26 21:32:01

I have two JavaScript arrays orig (the original array of objects) and update (the updated orig array of objects) that have the same length and contain objects, and I want to output the differences between the each pair of objects.

我有两个JavaScript数组orig(原始对象数组)和更新(更新的对象的orig数组),它们具有相同的长度并包含对象,我想输出每对对象之间的差异。

Example:

例:

var orig = [{enabled:"true", name:"Obj1", id:3},{enabled:"true", name:"Obj2", id:4}]; 

var update = [{enabled:"true", name:"Obj1", id:3}, {enabled:"true", name:"Obj2-updated", id:4}];

The output should be: name:"Obj2-updated"

输出应该是:name:“Obj2-updated”

I implemented something but it needs optimization...

我实现了一些东西,但它需要优化......

for(var prop=0; prop<orig.length; prop++) {
  for(prop=0; prop<update.length; prop++) {
    if(orig[prop].enabled != update.enabled) { console.log(update.enabled) }
    if(orig[prop].name != update[prop].name) { console.log(update[prop].name) }
    if(orig[prop].id != update[prop].id) { console.log(update[prop].id) }
  }
}

5 个解决方案

#1


12  

You could filter the keys and get a result set with the updated keys.

您可以过滤密钥并使用更新的密钥获取结果集。

var orig = [{ enabled: "true", name: "Obj1", id: 3 }, { enabled: "true", name: "Obj2", id: 4 }],
    update = [{ enabled: "true", name: "Obj1", id: 3 }, { enabled: "true", name: "Obj2-updated", id: 4 }],
    difference = [];

orig.forEach(function (a, i) {
    Object.keys(a).forEach(function (k) {
        if (a[k] !== update[i][k]) {
            difference.push({ id: update[i].id, key: k, value: update[i][k], index: i });
        }
    });
});

console.log(difference);

#2


6  

Assuming, that the property names of both are identical and the values are just primitives (no objects, arrays etc.):

假设两者的属性名称相同且值只是基元(没有对象,数组等):

Object.keys( orig )
      .forEach( (key) => {
        if( orig[ key ] !== update[ key ] ) {
          console.log( update[key] );
        }
      });

#3


3  

Use Object.keys to cycle through your object. Something like:

使用Object.keys循环遍历对象。就像是:

var orig = {
  enabled: "true",
  name: "Obj1",
  id: 3
};

var update = {
  enabled: "true",
  name: "Obj1-updated",
  id: 3
};

var diff = {};

Object.keys(orig).forEach(function(key) {
  if (orig[key] != update[key]) {
    diff[key] = update[key];
  };
})

console.log(diff);

#4


3  

You can use a single loop to go through the properties of the original object and check against the updated object to find out which properties have been modified.

您可以使用单个循环遍历原始对象的属性,并检查更新的对象以找出已修改的属性。

var orig = {
  enabled: "true",
  name: "Obj1",
  id: 3
};

var update = {
  enabled: "true",
  name: "Obj1-updated",
  id: 3
};

for (var key in orig) {
  if (orig[key] !== update[key]) {
    console.log(key + ': ' + update[key]);
  }
}

#5


0  

You could use a library like lodash or Ramda to do it in a concise manner. In the case of Ramda you could do: R.difference(update, orig);

您可以使用像lodash或Ramda这样的库来以简洁的方式完成它。在Ramda的情况下,你可以这样做:R.difference(update,orig);

#1


12  

You could filter the keys and get a result set with the updated keys.

您可以过滤密钥并使用更新的密钥获取结果集。

var orig = [{ enabled: "true", name: "Obj1", id: 3 }, { enabled: "true", name: "Obj2", id: 4 }],
    update = [{ enabled: "true", name: "Obj1", id: 3 }, { enabled: "true", name: "Obj2-updated", id: 4 }],
    difference = [];

orig.forEach(function (a, i) {
    Object.keys(a).forEach(function (k) {
        if (a[k] !== update[i][k]) {
            difference.push({ id: update[i].id, key: k, value: update[i][k], index: i });
        }
    });
});

console.log(difference);

#2


6  

Assuming, that the property names of both are identical and the values are just primitives (no objects, arrays etc.):

假设两者的属性名称相同且值只是基元(没有对象,数组等):

Object.keys( orig )
      .forEach( (key) => {
        if( orig[ key ] !== update[ key ] ) {
          console.log( update[key] );
        }
      });

#3


3  

Use Object.keys to cycle through your object. Something like:

使用Object.keys循环遍历对象。就像是:

var orig = {
  enabled: "true",
  name: "Obj1",
  id: 3
};

var update = {
  enabled: "true",
  name: "Obj1-updated",
  id: 3
};

var diff = {};

Object.keys(orig).forEach(function(key) {
  if (orig[key] != update[key]) {
    diff[key] = update[key];
  };
})

console.log(diff);

#4


3  

You can use a single loop to go through the properties of the original object and check against the updated object to find out which properties have been modified.

您可以使用单个循环遍历原始对象的属性,并检查更新的对象以找出已修改的属性。

var orig = {
  enabled: "true",
  name: "Obj1",
  id: 3
};

var update = {
  enabled: "true",
  name: "Obj1-updated",
  id: 3
};

for (var key in orig) {
  if (orig[key] !== update[key]) {
    console.log(key + ': ' + update[key]);
  }
}

#5


0  

You could use a library like lodash or Ramda to do it in a concise manner. In the case of Ramda you could do: R.difference(update, orig);

您可以使用像lodash或Ramda这样的库来以简洁的方式完成它。在Ramda的情况下,你可以这样做:R.difference(update,orig);