Search code examples
javascriptjqueryjsonoopjavascript-objects

JavaScript / jQuery: Compare 2 jSON objects and output result into new object


I have an api call that replies with an updated jSON object, I also have 1 static jSON object file. I am trying to compare a value in the object per teams with the same name.

So if Team John had 22 in the old file, and has 28 now, the new object should output Team John as 6. Subtracting the 2 and displaying the difference.

I have made a jsFiddle to help understand and update.

LATEST UPDATE: The answer has been solved by mscdeveloper! Check for his post and answer below.

UPDATE (not the answer): I have found a solution while searching in stackoverflow, this does EXACTLY what I want, but I lost the team's name in the process, how can I fix the code to where it doesn't delete it, I know it has something to do with the groupByTypeID function I have?

Updated jsFiddle: https://jsfiddle.net/kqmfsz9n/5/

   var obj1 = {
    "teams": [
    {
      name: 'Test 1',
      numMembers: '50'
    },
    {
      name: 'Test 2',
      numMembers: '12'
    }
  ]
};

var obj2 = {
    "teams": [
    {
      name: 'Test 1',
      numMembers: '75'
    },
    {
      name: 'Test 2',
      numMembers: '18'
    }
  ]
};

var newObj = {};
function groupByTypeID(arr) {
    var groupBy = {};
  jQuery.each(arr, function () {
    groupBy[this.name] = parseInt(this.numMembers);
  });
  return groupBy;
}

var userArrayGroups = groupByTypeID(obj2.teams);
var origArrayGroups = groupByTypeID(obj1.teams);

var newObj = [];
for (var prop in userArrayGroups) {
  newObj[prop] = userArrayGroups[prop] - origArrayGroups[prop];
  newObj.push(userArrayGroups[prop] - origArrayGroups[prop]);
  if (newObj[prop] == 0) {
    delete newObj[prop];
  }
}

console.log(newObj);

All help is appreciated! Thank you.


Solution

  •     var obj1 = {
            "teams": [
            {
              name: 'Test 1',
              numMembers: '50'
            },
            {
              name: 'Test 2',
              numMembers: '12'
            }
          ]
        };
    
        var obj2 = {
            "teams": [
            {
              name: 'Test 1',
              numMembers: '75'
            },
            {
              name: 'Test 2',
              numMembers: '18'
            }
          ]
        };
    
        var newObj = {};
    var items_arr=[];    //array of obj2 not exist in obj1
    
      if(obj1.teams){             //if exist array of teams obj1
         var o1teams = obj1.teams;
         if(obj2.teams){        //if exist array of teams obj2
            var o2teams = obj2.teams;
            for(var key2 in o2teams){
               var o2teams = obj2.teams;
               for(var key1 in o1teams){
                       if(o2teams[key2].name==o1teams[key1].name){
                           var numMembers_o1_int=parseInt(o1teams[key1].numMembers)||0;
                           var numMembers_o2_int=parseInt(o2teams[key2].numMembers)||0;
                           var result_numMembers_int=numMembers_o2_int-numMembers_o1_int;
                           var result_numMembers=result_numMembers_int+'';  //convert to string
                           var items_for_add=o1teams[key1];
                           items_for_add.numMembers=result_numMembers;
                           items_arr.push(items_for_add);
                       }
                }
            }
          }
      }
      newObj.items=items_arr;
    
      console.log(newObj);
    

    https://jsfiddle.net/mscdeveloper/uxv1t2a7/3/