0

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.

3
  • What about other types? (strings, date's, arrays, ...) Commented Oct 11, 2017 at 17:18
  • Since JSON is a string format all you have to do is a string comparison with ===.of your two JSON strings. Unless you are not talking about JSON, but that is what you say... Commented Oct 11, 2017 at 17:19
  • Wow so many vote downs for what? I am trying to get this solved but people these days vote down on anything for no reason without helping understand why. All I had was a simple questions, if I wanted to subtract numMembers from obj1 with obj2 numMembers, how would I go about this? Commented Oct 11, 2017 at 17:54

1 Answer 1

1
    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/

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

2 Comments

Although this is great for comparing the 2 objects, it doesn't subtract the numMembers from one another, and push the results into the new object or array.
I have updated my jsFiddle to give the proper answer, which still needs alittle bit more tweaking please check it

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.