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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T20:07:45+00:00 2026-06-15T20:07:45+00:00

Using Underscore.js, I’m trying to group a list of items multiple times, ie Group

  • 0

Using Underscore.js, I’m trying to group a list of items multiple times, ie

Group by SIZE then for each SIZE, group by CATEGORY…

http://jsfiddle.net/rickysullivan/WTtXP/1/

Ideally, I’d like to have a function or extend _.groupBy() so that you can throw an array at it with the paramaters to group by.

var multiGroup = ['size', 'category'];

Probably could just make a mixin…

_.mixin({
    groupByMulti: function(obj, val, arr) {
        var result = {};
        var iterator = typeof val == 'function' ? val : function(obj) {
                return obj[val];
            };
        _.each(arr, function(arrvalue, arrIndex) {
            _.each(obj, function(value, objIndex) {
                var key = iterator(value, objIndex);
                var arrresults = obj[objIndex][arrvalue];
                if (_.has(value, arrvalue))
                    (result[arrIndex] || (result[arrIndex] = [])).push(value);

My head hurts, but I think some more pushing needs to go here…

            });
        })
        return result;
    }
});

properties = _.groupByMulti(properties, function(item) {

    var testVal = item["size"];

    if (parseFloat(testVal)) {
        testVal = parseFloat(item["size"])
    }

    return testVal

}, multiGroup);
  • 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-15T20:07:46+00:00Added an answer on June 15, 2026 at 8:07 pm

    A simple recursive implementation:

    _.mixin({
      /*
       * @mixin
       *
       * Splits a collection into sets, grouped by the result of running each value
       * through iteratee. If iteratee is a string instead of a function, groups by
       * the property named by iteratee on each of the values.
       *
       * @param {array|object} list - The collection to iterate over.
       * @param {(string|function)[]} values - The iteratees to transform keys.
       * @param {object=} context - The values are bound to the context object.
       * 
       * @returns {Object} - Returns the composed aggregate object.
       */
      groupByMulti: function(list, values, context) {
        if (!values.length) {
          return list;
        }
        var byFirst = _.groupBy(list, values[0], context),
            rest    = values.slice(1);
        for (var prop in byFirst) {
          byFirst[prop] = _.groupByMulti(byFirst[prop], rest, context);
        }
        return byFirst;
      }
    });
    

    Demo in your jsfiddle

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

Sidebar

Related Questions

I'm using backbone.js, underscore.js and jQuery. I took the following sample code from http://jsfiddle.net/thomas/C9wew/6/
I'm trying to render a Backbone.js collection as a select list using an Underscore.js
Using underscore.js is there a way to breakout of the each if a certain
Using jQuery / Underscore, Given the following list. How can I find all .msg
I'm trying to replace a period in a string to an underscore using the
I am trying to filter through this javascript object using underscore.js, but I don't
I'm using an underscore character below each form field and related label in my
I'm trying to render an html table using underscore template engine. First I have
I'm trying to build a similar version of Rails ActiveRecord in Javascript, using underscore
I'm trying to learn Backbone.js with Underscore.js and got into trouble. I'm using Grails

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.