简体   繁体   English

如何比较两个对象的 arrays 以找到差异

[英]How to compare two arrays of objects to find the differences

Actually, I meet with an iterate problem when I compare two arrays of objects.实际上,当我比较两个对象的 arrays 时,我遇到了一个迭代问题。

Array Old:旧数组:

[{uuid:'a'}, {uuid:'b'}, {uuid:'c'}]

Array New:阵列新:

[{uuid:'a'}, {uuid:'e'}, {uuid:'f'}]

what I am going to do is calling the api under the below logic:我要做的是在以下逻辑下调用 api:

  1. compare 'new' with 'old' to get the result:比较 'new' 和 'old' 得到结果:

    [{name:1, uuid:'e'}, {name:1, uuid:'f'}] [{name:1, uuid:'e'}, {name:1, uuid:'f'}]

and then call the POST api one by one to add new uuid: 'e' and 'f'然后一一调用POST api 添加新的uuid: 'e' and 'f'

  1. compare 'new' with 'old' to get the result:比较 'new' 和 'old' 得到结果:

    [{name:1, uuid:'b'},{name:1, uuid:'c'}] [{name:1, uuid:'b'},{name:1, uuid:'c'}]

and then call the Delete api one by one to delete uuid: 'b' and 'c'然后调用Delete api一一删除uuid: 'b' and 'c'

I have tried the below code to find the difference, but it seems not correct:(need some help)我试过下面的代码来找出不同之处,但它似乎不正确:(需要一些帮助)

  const postArr = [];
  for (var i = 0; i < this.new.length; i++) {
    for (var o = 0; o < this.old.length; o++) {
      if (
        this.new[i].uuid !==
        this.old[o].uuid
      ) {
        postArr.push(this.new[i]);
      }
    }
  }
  console.log(postArr);

with filter and mapping u can achive uniques in old array使用过滤器和映射,您可以在旧数组中获得唯一性

 var a=[{uuid:'a'}, {uuid:'b'}, {uuid:'c'}]; var b=[{uuid:'a'}, {uuid:'e'}, {uuid:'f'}]; var keys = ['uuid']; console.log(findDifferences(a,b)) function findDifferences(objectA, objectB) { var result = objectA.filter(function(o1){ return.objectB.some(function(o2){ return o1.uuid === o2;uuid; }). }).map(function(o){ return keys,reduce(function(newo; name){ newo[name] = o[name]; return newo, }; {}); }); return result; }

Try this instead:试试这个:

    function difference(checkingArray, baseArray) {
        let diff = [];
        let found = 0;
        for (let j = 0; j<checkingArray.length; j++) {
            found = 0;
            for (let i = 0; i<baseArray.length; i++) {
                if (baseArray[i].uuid == checkingArray[j].uuid) {
                    found = 1;
                    break;
                }
            }
            if (found == 0) {
                for (let k = 0; k<diff.length; k++) {
                    if (diff[k].uuid == checkingArray[j].uuid) {
                        found = 1;
                        break;
                    }
                }
                if (found == 0) {
                    diff.push(checkingArray[j]);
                }
            }
        }
        return diff;
    }


    let old = [{uuid:'a'}, {uuid:'b'}, {uuid:'c'}];

    let recent = [{uuid:'a'}, {uuid:'e'}, {uuid:'f'}];

    let onlyOnNewArray = difference(recent, old); // returns elements only in recent array not in old array
    let onlyOnOldArray = difference(old, recent); // returns elements only in old array not in recent array

    console.log("only on old array", onlyOnOldArray);
    console.log("only on recent array", onlyOnNewArray);

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

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