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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T18:10:55+00:00 2026-06-18T18:10:55+00:00

I have a collection which has a bunch of models which look like the

  • 0

I have a collection which has a bunch of models which look like the following example:

{
    metricName : 'clicks',
    metricValue : 100
}

What I want is to search through the collection and consolidate all the models that have the same metricName into one model which has the sum of all the values.

So

{
    metricName : 'clicks',
    metricValue : 100
}

{
    metricName : 'otherMetric',
    metricValue : 100
}

{
    metricName : 'clicks',
    metricValue : 100
}

turns into

{
    metricName : 'clicks',
    metricValue : 200
}

{
    metricName : 'otherMetric',
    metricValue : 100
}

I’m trying to solve this as efficiently and easily as possible by using underscore.

I can see one solution is to use _.each and loop through and keep track of metric names while summing the values and eliminating dupes (sounds lame).

My question is if there is another, better way to do this using one of underscores other methods. I can see maybe map, find, findwhere, filter, etc might be options but I’m not sure.

  • 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-18T18:10:56+00:00Added an answer on June 18, 2026 at 6:10 pm

    You could do something cute with groupBy and reduce:

    collection.reset(_.map(_.groupBy(collection.toJSON(), 'metricName'), function(group) {
      return _.reduce(group, function(memo, item) {
        if(!memo) return item;
        memo.metricValue+=item.metricValue;
        return memo;
      });
    }));
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a backbone collection which has a bunch of models with date attributes
I have the following collection which i would like to bind to a combobox:
I have a mongo collection which has documents with two fields fieldA and fieldB,
I have a list: Collection users which has around 100K+ records of users (all
I have a small data collection app which has a single button to start
I have a Customer object which has a collection of ContactNumbers. Is it possible
I have a Person class which has a String collection of aliases representing additional
I have an APVendor class, which has a collection of APInvoice classes and a
We are currently implementing a solution where we have a collection which we want
I have a collection in which each each item has a property ID. There

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.