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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T12:09:53+00:00 2026-06-13T12:09:53+00:00

Only just introduced to _Underscore so this may be totally nonsense but I’m wondering

  • 0

Only just introduced to _Underscore so this may be totally nonsense but I’m wondering if the library can indeed do what I want.

I have an object containing 5 items:

Name, v2007,v2008,v2009,v2010

I simply want to go through the entire object and get the SUM of each distinct value in Name (much similar to SQL’s GroupBy and SUM).

I have the following:

var distincts = [];
$.each(tmp, function (key, value) {
    $.each(value, function (fieldName, fieldValue) {
        if (fieldName == 'Name') {
            if ($.inArray(fieldValue, distincts) == -1) {
                distincts.push(fieldValue);
            }
        }
    });
});

$.each(distincts, function (a, b) {
    var t = _.where(tmp, {Name: b});
    var sum = _.reduce(t.v2007, function (memo, num) {
        return memo + num;
    }, 0)
    console.log(sum);
});

Which is returning 0 all the time, in fact I’m not sure how to retrieve a SUM of fields v2011, v2013 other than hard-coding them in which is simply not practical!

Can someone help me a little here, is there a simple way of getting the _Underscore library to SUM all fields (that, say, start with ‘v’) based on a Group of the Name?

  • 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-13T12:09:53+00:00Added an answer on June 13, 2026 at 12:09 pm

    I think this does what you want:

    var groups = _.groupBy(function(name) { return name.charAt(0) });
    var sums = _.map(groups, function(group) {
        return _.reduce(group, function (a, b) { return a+b; }, 0);
    });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

With php file_get_contents() i want just only the post and image. But it's get
I want to have the look of UITableViewCellStyleValue1 but instead of just only having
I'm hoping you guys can answer me a question? I've only just started out
I just wonder if it is possible to only use CSS but not javascript
I often find I want to write code something like this in C#, but
I'm a largely self-taught front-end developer only just making the transition into back-end development
I'm doing a .NET unit as part of my studies. I've only just started,
I need to check if a link (a-tag) is the only content (not just
When TimerCallback is static, it will execute only once and just hang and will
I just started out with jQuery and really only need it for some very

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.