2

I have two arrays below. array 1:

{
  "0":{
  "countries":{
     "BDI":{
        "count":1
     },
     "IRN":{
        "count":1
     }
  },
  "checkId":"16835659691517226105"
},
 "1":{
  "countries":{
     "ZAF":{
        "count":2
     }
  },
  "checkId":"144165083478491226"
 }
}

array2:

{
 "0":{
  "countries":{
     "AIA":{
        "count":2
     }
  },
  "checkId":"144165083478491106"
 },
  "1":{
  "countries":{
     "BDI":{
        "count":1
     },
     "IRN":{
        "count":1
     },
     "ATA":{
        "count":5
     }
  },
  "checkId":"16835659691517226105"
}
 
}

I want to find the mismatch and common element between the two arrays. currently, I am executing two for loops to find the matching element between two array-based on checkids but I am not able to find the non-common elements from these two. some code snippets

  array1.forEach(each => {
array2.forEach(compareTask => {
  var teastEach = Object.entries(compareTask.countries);
  if (each.checkId === compareTask.checkId) {
    firstCount = each.count
    secondCount = compareTask.count
    countDifference =  secondCount - firstCount

......

I am able to get the common checkids but not getting the non-common checkids. expected output:

    {
      "0":{
  "countries":{
     "ZAF":{
        "count":2
     }
  },
  "checkId":"144165083478491226"
  },
 "1":{
  "countries":{
     "AIA":{
        "count":2
     }
  },
  "checkId":"144165083478491106"
  }
  }
10 Rep
  • 2,217
  • 7
  • 19
  • 33
Rhea
  • 381
  • 1
  • 7
  • 22
  • have a look at this: https://stackoverflow.com/a/3198202/12361700 – Alberto Sinigaglia Aug 11 '20 at 00:20
  • show us the desired output for mismatching arrays and for matching arrays. – Sascha Aug 11 '20 at 00:20
  • also, that you are posting are objects, and your code is working on arrays.... so? – Alberto Sinigaglia Aug 11 '20 at 00:21
  • Why write your own code for this? Just search npm and you'll find _several_ libraries that can do object diffing, which is a non-trivial task and any code you _don't_ have to write yourself is time you can spend on the code you _do_ have to write yourself. – Mike 'Pomax' Kamermans Aug 11 '20 at 00:29
  • @Berto99 Those are Objects. I have used JSON.stringify(obj) to print the value for array – Rhea Aug 11 '20 at 03:47
  • #Sascha iI have posted expected output above.. here it is { "0":{ "countries":{ "ZAF":{ "count":2 } }, "checkId":"144165083478491226" }, "1":{ "countries":{ "AIA":{ "count":2 } }, "checkId":"144165083478491106" } – Rhea Aug 11 '20 at 03:54

1 Answers1

2

From the comments it looks like you could use Map()

object1 = { "0": { countries: { BDI: { count: 1, }, IRN: { count: 1, }, }, checkId: "16835659691517226105", }, "1": { countries: { ZAF: { count: 2, }, }, checkId: "144165083478491226", }, };
object2 = { "0": { countries: { AIA: { count: 2, }, }, checkId: "144165083478491106", }, "1": { countries: { BDI: { count: 1, }, IRN: { count: 1, }, ATA: { count: 5, }, }, checkId: "16835659691517226105", }, };

map = new Map();
arr = [Object.values(object1), Object.values(object2)].flat();
result = [
  ...arr
    .reduce((r, o) => {
      const dupli = r.get(o.checkId);
      dupli ? r.delete(o.checkId) : r.set(o.checkId, o);
      return r;
    }, new Map())
    .values(),
];
console.log(result);
Sven.hig
  • 4,449
  • 2
  • 8
  • 18