search

Home  >  Q&A  >  body text

javascript - There are differences in the formats of the two arrays, and the problem of comparison and deduplication

arr1 =[{"id":1,"a":2},{"id":2,"c":3},{"id":3,"d":3},{"id":4,"d":3}];
arr2 =[{"id":3,"ed":1111,"vvv":121},{"id":4,"fa":2222},{"id":5,"sa":2222}];

The array formats of arr1 and arr2 are different, but id3 and id4 in arr2 are the same as arr1.
How to realize that after comparing the two arrays, it is found that the IDs are duplicated, and delete the id 3 and id4 in arr1 The entire object?

某草草某草草2750 days ago580

reply all(1)I'll reply

  • 巴扎黑

    巴扎黑2017-05-19 10:32:29

    var result = arr1.filter(function(element1) {
       return !arr2.find(function(element2) { 
           return element2.id == element1.id;
       });
    });
    console.log(result);

    reply
    0
  • Cancelreply