How to compare two arrays of objects to find the differences

Lincoln347

Actually, I meet with an iterate problem when I compare two arrays of objects.

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:

  1. compare 'new' with 'old' to get the result:

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

and then call the POST api one by one to add new uuid: 'e' and 'f'

  1. compare 'new' with 'old' to get the result:

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

and then call the Delete api one by one to delete 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);
pc_coder

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;
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Compare two objects' properties to find differences?

How to find differences between two JavaScript arrays of objects?

How to best compare two multi-level objects in JavaScript in order to find differences?

compare two arrays of objects

Compare two large arrays of objects effectively and find the differences

Compare two arrays of objects JS

How to compare two arrays and remove duplicate objects by complete iteration

How to deep compare two javascript objects and return all the differences including new added arrays and with the same original format?

How to compare two arrays with objects to filter the ones that are similar (vanilla JS)

compare 2 Java script objects to find differences

How to compare two objects?

How to compare two arrays of objects only once?

How to compare two arrays of objects and delete the duplicates in javascript

How to compare two arrays and find the the match (0/1) in an array in Clickhouse

Compare two spreadsheets in a workbook, find the differences and record the differences on the first spreadsheet

How to compare arrays and objects to find a match PHP

Compare two arrays and find the differences

How to find all OBJECTS that intersect two arrays?

How to find the differences between two arrays in JavaScript?

How do you compare two arrays to get the count of similar objects?

The simplest way to find the differences between two arrays

How to compare two big arrays of objects on identity

How to compare values in two arrays to find one or more matches in? [jQuery]

How to compare two arrays and find the indices where the elements differ?

Compare two arrays of objects in Javascript

Compare two different images and find the differences

How to compare objects of two different arrays in javascript

Without built-in functions how to find differences in two arrays

How to compare two arrays of objects and get the values that differ in React?

TOP Ranking

HotTag

Archive