Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8464775
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T14:51:14+00:00 2026-06-10T14:51:14+00:00

Using jQuery – I would like to compare 2 JSON arrays: origArray comes from

  • 0

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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T14:51:16+00:00Added an answer on June 10, 2026 at 2:51 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Using jQuery and backbone.js, I would like to separate the JavaScript from the HTML
Using jQuery Nearest Element , I would like to select elements in a circular
Using jQuery UI I have two radio buttons, Approve/Reject, which I would like to
Using jQuery, I would like to capture a keyboard event that is: before the
Using jQuery, I make these changes in the DOM: $(document).ready(function() { $('.editable').click(function() { var
Using jQuery (1.7), and not jQueryUI - (how) would it be possible to accomplish
Using jquery 1.7.1, I'm using json to populate the dropdown. The code works just
Using Jquery AJAX to get a value from a PHP script. This line works
Using jquery is it better to create a DOM element like this:- function create(options)
Using jQuery, what would be the most efficient way to find the first two

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.