1

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);
1
  • 2
    You should use the array filter method to achieve this. Commented Apr 19, 2020 at 8:54

2 Answers 2

2

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

Sign up to request clarification or add additional context in comments.

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.