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 3623422
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T23:22:53+00:00 2026-05-18T23:22:53+00:00

Hi I have a javascript array object rapresenting the amount of items sold in

  • 0

Hi
I have a javascript array object rapresenting the amount of items sold in a given country, like this:

var data = [{'c1':'USA', 'c2':'Item1', 'c3':100}, 
            {'c1':'Canada', 'c2':'Item1', 'c3':120},
            {'c1':'Italy', 'c2':'Item2', 'c3':140},
            {'c1':'Italy', 'c2':'Item2', 'c3':110}]

I need to avoid duplicates (as you may see, the last two ‘records’ have the same Country and the same Item) and sum the amounts; if I was getting data from a database I would use the DISTINCT SUM clause, but what about it in this scenario? Is there any good jquery trick?

  • 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-05-18T23:22:54+00:00Added an answer on May 18, 2026 at 11:22 pm

    You could use an object as a map of distinct values, like this:

    var distincts, index, sum, entry, key;
    distincts = {};
    sum = 0;
    for (index = 0; index < data.length; ++index) {
        entry = data[index];
        key = entry.c1 + "--sep--" + entry.c2;
        if (!distincts[key]) {
            distincts[key] = true;
            sum += entry.c3;
        }
    }
    

    How that works: JavaScript objects are maps, and since access to properties is an extremely common operation, a decent JavaScript implementation tries to make property access quite fast (by using hashing on property keys, that sort of thing). You can access object properties using a string for their name, by using brackets ([]), so obj.foo and obj["foo"] both refer to the foo property of obj.

    And so:

    1. We start with an object with no properties.
    2. As we loop through the array, we create unique key from c1 and c2. It’s important that the “–sep–” string be something that cannot appear in c1 or c2. If case isn’t significant, you might throw a .toLowerCase in there.
    3. If distincts already has a value for that key, we know we’ve seen it before and we can ignore it; otherwise, we add a value (true in this case, but it can be just about anything other than false, undefined, 0, or "") as a flag indicating we’ve seen this unique combination before. And we add c3 to the sum.

    But as someone pointed out, your last two entries aren’t actually the same; I’m guessing that was just a typo in the question…

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

Sidebar

Related Questions

I'm creating my own JavaScript Array-like object and I have methods that call closures.
In javascript I have an array as follows: var foo = [2, 2, 4,
I have an external JavaScript that contains: function random_imglink(){ var myimages=new Array() myimages[1]="http://sevir.sitegoz.com/jchs/Banner1.png" myimages[2]="http://sevir.sitegoz.com/jchs/banner2.png"
I have a one-dimensional array of strings in JavaScript that I'd like to turn
I have a Javascript object called TweenManager which contains an array of Tween objects.
I need to create object parameter some thing like this sample_data=[{ name: 'new', data:
I hope someone can help me with this Javascript. I have an Object called
I have an array I've created in JavaScript. The end result comes out to
We have a JavaScript function named move which does just windows.location.href = any given
I have been playing with javascript arrays and I have run into, what I

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.