2

I wanted to get the difference between two arrays which is nested.

let firstArray = {
  "family": "BLUE",
  "globalThreshold": "2.0",
  "levelData": [
    {
      "name": "India",
      "value": "4.0",
      "count": [
        {
          "id": "21",
          "countName": "ABC",
          "countThreshold": "7.0"
        },
        {
          "id": "22",
          "workscopeName": "DEF",
          "countThreshold": "4242"
        }
      ]
    },
    {
      "name": "FEDERAL EXPRESS CORPORATION",
      "value": "1.0",
      "count": [
        {
          "id": "5",
          "countName": "ABC",
          "countThreshold": "2.0"
        },
        {
          "id": "6",
          "countName": "DEF",
          "countThreshold": "3.0"
        }
      ]
    }
  ]
}



 let changedArray= {
      "family": "BLUE",
      "globalThreshold": "2.0",
      "levelData": [
        {
          "name": "India",
          "value": "5",
          "count": [
            {
              "id": "21",
              "countName": "ABC",
              "countThreshold": "7.0"
            },
            {
              "id": "22",
              "workscopeName": "DEF",
              "countThreshold": "4242"
            }
          ]
        },
        {
          "name": "FEDERAL EXPRESS CORPORATION",
          "value": "1.0",
          "count": [
            {
              "id": "5",
              "countName": "ABC",
              "countThreshold": "60"
            },
            {
              "id": "6",
              "countName": "DEF",
              "countThreshold": "3.0"
            }
          ]
        }
      ]
    }

Expected result:

let finalArray = {
  "family": "BLUE",
  "globalThreshold": "2.0",
  "levelData": [
    {
      "name": "India",
      "value": "5",
      "count": []
    },
    {
      "name": "FEDERAL EXPRESS CORPORATION",
      "value": "1.0",
      "count": [
        {
          "id": "5",
          "countName": "ABC",
          "countThreshold": "60"
        } 
      ]
    }
  ]
}

I would like to track the diffference based on the value property inside 'levelData' array and countThreshold inside count array.

i tried repeating two arrays in for loop but was not able to repeat Any simultaneously as its nested.Do let me know any quick approach.. Thanks

forgottofly
  • 2,729
  • 11
  • 51
  • 93
  • Possible duplicate of [How to compare arrays in JavaScript?](https://stackoverflow.com/questions/7837456/how-to-compare-arrays-in-javascript) – VilleKoo Mar 09 '18 at 07:07
  • In Javascript the `{}` makes an object. So it may be a duplicate of: [Object comparison in javascript](https://stackoverflow.com/questions/1068834/object-comparison-in-javascript). Because you have more objects and arrays inside the "levelData" array, you want to do a deep comparison. – fdelia Mar 09 '18 at 07:10

1 Answers1

1

Try using map to iterate for each levelData finding their respective in firstArray by name then filter by matching its properties:

let firstArray={"family":"BLUE","globalThreshold":"2.0","levelData":[{"name":"India","value":"4.0","count":[{"id":"21","countName":"ABC","countThreshold":"7.0"},{"id":"22","workscopeName":"DEF","countThreshold":"4242"}]},{"name":"FEDERAL EXPRESS CORPORATION","value":"1.0","count":[{"id":"5","countName":"ABC","countThreshold":"2.0"},{"id":"6","countName":"DEF","countThreshold":"3.0"}]}]};
let changedArray={"family":"BLUE","globalThreshold":"2.0","levelData":[{"name":"India","value":"5","count":[{"id":"21","countName":"ABC","countThreshold":"7.0"},{"id":"22","workscopeName":"DEF","countThreshold":"4242"}]},{"name":"FEDERAL EXPRESS CORPORATION","value":"1.0","count":[{"id":"5","countName":"ABC","countThreshold":"60"},{"id":"6","countName":"DEF","countThreshold":"3.0"}]}]}

var arr = changedArray.levelData.map(ele => {
   var count = firstArray.levelData.find(x => x.name == ele.name).count;
   ele.count = ele.count.filter(x => !count.some(y => x.id == y.id && x.countThreshold == y.countThreshold));
   return ele;
 })
 
 console.log(arr);
guijob
  • 4,413
  • 3
  • 20
  • 39
  • Thanks @guijob I tried implementing with the actual JSON but not giving the filtered items.Can you pls have a look https://plnkr.co/edit/usErcD?p=preview I'm looking at difference in `customerThreshold` and `wokscopeThreshold` – forgottofly Mar 09 '18 at 08:27
  • https://plnkr.co/edit/VqYe7x?p=preview Changed `customerThreshold` to 32033 and the code breaks.Any idea where I'm doing wrong – forgottofly Mar 09 '18 at 08:30
  • it's throwing error because I was searching a `spyData` in another array by `name` and you change it to `customerThreshold`. and when it was not found, there are no `workscope` property, which results in error. in this case you have to check if obj is null: [plnkr](https://plnkr.co/edit/avddEvl2WuvynyevNipb?p=preview). – guijob Mar 09 '18 at 08:52
  • Thanks@guijob. But I'm not able to get the difference in workscope level ie., `'wokscopeThreshold': '88'` .Currently its showing all workscopes for FRANCE – forgottofly Mar 09 '18 at 09:13
  • https://plnkr.co/edit/q4HMEq2qV9HNn1tBywDv?p=preview Can you please check how to remove workscopes in FRANCE as they are same – forgottofly Mar 09 '18 at 09:18
  • Also is there any way to get the count of changed records from the above logic ? – forgottofly Mar 09 '18 at 11:49
  • @forgottofly workplaces are the same but the customer threshold aren't so you are even comparing them. if you like to compare customer threshold for same name, [check if out](https://plnkr.co/edit/hjTA3C5Y3tuUmwGYYXD5?p=preview). – guijob Mar 09 '18 at 15:25
  • Thanks@guijob .Yes want to compare both customerThreshold as well as wokscopeThreshold .But in the latest plunker there's a difference in workscope level of CANADA ie., 'wokscopeThreshold': '88' and that object is not reflected in workscopes array.Can yu please check – forgottofly Mar 10 '18 at 06:14