2

Using jQuery - I would like to compare 2 JSON arrays:

origArray comes from the database/C# API:

var origArray = [   {
        "TypeName": "Single",
        "TypeID": "3121",
        "TypeCount": "2"
        },
    {
        "TypeName": "Double",
        "TypeID": "4056",
        "TypeCount": "2"
        },
    {
        "TypeName": "Family",
        "TypeID": "5654",
        "TypeCount": "4"
        }
];

userArray is gathered from user input:

var userArray = [   {
        "TypeID": "3121",
        "TypeCount": "2"
        },
    {
        "TypeID": "4056",
        "TypeCount": "3"
        },
    {
        "TypeID": "3121",
        "TypeCount": "3"
        }
];

What I would like to do, is loop through the userArray, and "group" by the TypeID, and Sum the TypeCount.

So in the example above:

TypeID: 3121
TypeCount: 5 (ie. 2 + 3)

TypeID: 4056
TypeCount: 3

Meaning there would be a difference of 3 over for TypeID 3121 and 1 over for 4056.

Is there any way of getting that information out of either jQuery or native Javascript (that would work cross-browser)?

Thanks for any help,

Mark

5 Answers 5

1

Define a function to group by each array by TypeID and sum TypeCount:

function groupByTypeID (arr) {
    var groupBy = {};
    $.each(arr, function () { 
      var currentCount = groupBy[this.TypeID] || 0; 
      groupBy[this.TypeID] = currentCount + parseInt(this.TypeCount);
    });
    return groupBy;
}

Then, group both arrays, and take their differences:

var userArrayGroups = groupByTypeID(userArray);
var origArrayGroups = groupByTypeID(origArray);

var diff = {};
for (var prop in userArrayGroups) {
  diff[prop] = userArrayGroups[prop] - origArrayGroups[prop];
}

diff will then hold the following:

{
  3121: 3
  4056: 1
}

DEMO.

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

Comments

1

If you are familiar with C# this js library - linqjs. It contains implementations of all .NET 4.0 LINQ methods and many extra methods.

Comments

1

You can do it with Underscore:

var result = _.chain(userArray)
    .groupBy(function(value) {
      // group by the TypeId
      return value.TypeID;
    })
    .map(function(value) {
      // iterate over every group
      var addition = _.chain(value)
        .pluck("TypeCount") // create an array with TypeCount values
        .reduce(function(memo, num) {
          return Number(memo) + Number(num); // add all the TypeCount values
        })
        .value();

      return {
        "TypeID": value[0].TypeID, // TypeID
        "TypeCount": addition // total addition for this TypeID
      };
    })
    .value();

Working example here: http://livecoding.io/3498441

Comments

1

I'll have a shot at it...

var mergeArray = []
var index;

for (var i = userArray.length - 1; i >= 0; i--) {
    index = findByTypeID(mergeArray, userArray[i].TypeID);
    if(!index){
        mergeArray[index].TypeCount += parseInt(userArray[i].TypeCount)
    } else{
        mergeArray.push({
            "TypeID": userArray[i].TypeID,
            "TypeCount": parseInt(userArray[i].TypeCount)
        });
   }
};

function findByTypeID(arr, id){
    for (var i = arr.length - 1; i >= 0; i--) {  
        if(arr[i].TypeID == id)
             return i;
    };
    return -1;
}

This gives you your desired data structure output inside mergeArray

Comments

-1
result = {};
for (var i in userArray) {
    var elem = userArray[i]
    if (!result[elem.TypeID])
      result[elem.TypeID] = elem.TypeCount;
    else
      result[elem.TypeID] += elem.TypeCount;
}

return result;

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.