简体   繁体   English

检查javascript中两个对象数组之间的差异

[英]check the difference between two arrays of objects in javascript

I need some help.我需要帮助。 How can I get the array of the difference on this scenario:我怎样才能得到这种情况下的差异数组:

var b1 = [
  { id: 0, name: 'john' }, 
  { id: 1, name: 'mary' }, 
  { id: 2, name: 'pablo' }, 
  { id: 3, name: 'escobar' } 
]; 

var b2 = [
  { id: 0, name: 'john' }, 
  { id: 1, name: 'mary' }
];

I want the array of difference:我想要不同的数组:

// [{ id: 2, name: 'pablo' }, { id: 3, name: 'escobar' }]

How is the most optimized approach?最优化的方法是怎样的?

I´m trying to filter a reduced array.. something on this line:我正在尝试过滤一个简化的数组..这一行的内容:

var Bfiltered = b1.filter(function (x) {
return x.name !== b2.reduce(function (acc, document, index) {
    return (document.name === x.name) ? document.name : false
},0)

}); });

console.log("Bfiltered", Bfiltered);
// returns { id: 0, name: 'john' }, { id: 2, name: 'pablo' }, { id: 3, name: 'escobar' } ]

Thanks,谢谢,

Robot机器人

.Filter() and .some() functions will do the trick .some() .Filter().some()函数可以解决这个问题

 var b1 = [ { id: 0, name: 'john' }, { id: 1, name: 'mary' }, { id: 2, name: 'pablo' }, { id: 3, name: 'escobar' } ]; var b2 = [ { id: 0, name: 'john' }, { id: 1, name: 'mary' } ]; var res = b1.filter(item1 => !b2.some(item2 => (item2.id === item1.id && item2.name === item1.name))) console.log(res);

You can use filter to filter/loop thru the array and some to check if id exist on array 2您可以使用filter过滤/循环遍历数组,并使用some来检查数组 2 上是否存在 id

 var b1 = [{ id: 0, name: 'john' }, { id: 1, name: 'mary' }, { id: 2, name: 'pablo' }, { id: 3, name: 'escobar' } ]; var b2 = [{ id: 0, name: 'john' }, { id: 1, name: 'mary' }]; var result = b1.filter(o => !b2.some(v => v.id === o.id)); console.log(result);


Above example will work if array 1 is longer.如果数组 1 更长,则上面的示例将起作用。 If you dont know which one is longer you can use sort to arrange the array and use reduce and filter .如果您不知道哪个更长,您可以使用sort来排列数组并使用reducefilter

 var b1 = [{ id: 0, name: 'john' }, { id: 1, name: 'mary' }, { id: 2, name: 'pablo' }, { id: 3, name: 'escobar' } ]; var b2 = [{ id: 0, name: 'john' }, { id: 1, name: 'mary' }]; var result = [b1, b2].sort((a,b)=> b.length - a.length) .reduce((a,b)=>a.filter(o => !b.some(v => v.id === o.id))); console.log(result);

Another possibility is to use a Map , allowing you to bring down the time complexity to O(max(n,m)) if dealing with a Map -result is fine for you:另一种可能性是使用Map ,如果处理Map结果适合您,则允许您将时间复杂度降低到O(max(n,m))

 function findArrayDifferences(arr1, arr2) { const map = new Map(); const maxLength = Math.max(arr1.length, arr2.length); for (let i = 0; i < maxLength; i++) { if (i < arr1.length) { const entry = arr1[i]; if (map.has(entry.id)) { map.delete(entry.id); } else { map.set(entry.id, entry); } } if (i < arr2.length) { const entry = arr2[i]; if (map.has(entry.id)) { map.delete(entry.id); } else { map.set(entry.id, entry); } } } return map; } const arr1 = [{id:0,name:'john'},{id:1,name:'mary'},{id:2,name:'pablo'},{id:3,name:'escobar'}]; const arr2 = [{id:0,name:'john'},{id:1,name:'mary'},{id:99,name:'someone else'}]; const resultAsArray = [...findArrayDifferences(arr1,arr2).values()]; console.log(resultAsArray);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM