最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Compare two JSON arrays in JQuery or Javascript - Stack Overflow

programmeradmin0浏览0评论

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

origArray es 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

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

origArray es 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

Share Improve this question edited Aug 28, 2012 at 14:04 Gabriel Florit 2,9181 gold badge22 silver badges37 bronze badges asked Aug 28, 2012 at 14:02 MarkMark 7,82826 gold badges92 silver badges148 bronze badges
Add a ment  | 

5 Answers 5

Reset to default 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.

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

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

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

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;
发布评论

评论列表(0)

  1. 暂无评论