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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T09:48:29+00:00 2026-06-14T09:48:29+00:00

If I had data that looked like this: harvest = [{type: apple, color: green,

  • 0

If I had data that looked like this:

harvest = [{type: "apple", color: "green", value: 1}, 
           {type: "apple", color: "red", value: 2}, 
           {type: "grape", color: "green", value: 3},
           {type: "grape", color: "red", value: 4 }]

I could sum it by various attributes using d3’s nest.rollup() function:

sum_by = "color";

rollup = d3.nest().key(function(d) {
  return d[sum_by];
}).rollup(function(d) {
  return d3.sum(d, function(g) {
    return g.value;
  });
}).entries(harvest);

Giving me this:

rollup = [{key: "green", values: 4},
          {key: "red", values: 6}]

Which is just what I want.

However the values in my data consist of arrays, all of equal length:

harvest = [{type: "apple", color: "green", values: [1,2,3,4]}, 
           {type: "apple", color: "red", values: [5,6,7,8]}, 
           {type: "grape", color: "green", values: [9,10,11,12]},
           {type: "grape", color: "red", values: [13,14,15,16] }]

Is it possible to combine these in a similar way? To give for example:

rollup = [{key: "green", values: [10,12,14,16]},
          {key: "red", values: [18,20,22,24]}]

I feel this is probably possible using a d3 rollup function (but it doesn’t necessarily have to be done using d3).

RESOLUTION

Thanks to the efforts of @meetamit and @Superboggly I have three solutions:

Version 1 (preferred because it uses reduce() just once and map() just once):

function sumArrays(group) {
  return group.reduce(function(prev, cur, index, arr) {
    return {
      values: prev.values.map(function(d, i) {
        return d + cur.values[i];
      })
    };
  });
}

Version 2:

function sumArrays(group) {
  return group.map(function(h) {
    return h.values;
  }).reduce(function(prev, cur, index, arr) {
    return prev.map(function(d, i) {
      return d + cur[i];
    });
  });
}

Version 3 (for interest because array length can vary):

function sumArrays(group) {
  return group.reduce(function(prev, cur, index, arr) {
    return prev.map(function(d, i) {
      return d + cur.values[i];
    });
  }, [0, 0, 0, 0]);
}

Called like this:

function rollupArrays() {
  return d3.nest().key(function(d) {
    return d[sum_by];
  }).rollup(sumArrays).entries(harvest);
}

And converted to CoffeeScript:

rollupArrays = ->
  d3.nest().key (d) ->
    d[sum_by]
  .rollup(sumArrays).entries(harvest)

sumArrays = (group) ->
  group.reduce (prev, cur, index, arr) ->
    values: prev.values.map (d,i) ->
      d + cur.values[i]

UPDATE

This method isn’t suitable if the function must run, even with one input row. See Part II

  • 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-14T09:48:30+00:00Added an answer on June 14, 2026 at 9:48 am

    One solution uses [].reduce() and [].map():

    // eg: sumArrays([ [1,2,3,4], [5,6,7,8] ]);// <- outputs [6, 8, 10, 12]
    function sumArrays(arrays) {
      return arrays.reduce(
        function(memo, nums, i) {
          if(i == 0)
            return nums.concat();
          else
            return memo.map(
              function(memoNum, i) {
                return memoNum + nums[i];
              }
            );
        },
        [ ]// Start with empty Array for memo
      );
    }
    

    Both reduce and map are not native in old JS, so best use a module (underscore, or maybe there’s a d3 equivalent to reduce, but I haven’t seen it).

    EDIT

    Using it in your code:

    sum_by = "color";
    
    rollup = d3.nest().key(function(d) {
      return d[sum_by];
    }).rollup(function(d) {
      var arraysToSum = d.map(function(g) { return g.values; });
      return sumArrays(arraysToSum)
    }).entries(harvest);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

We had a View Model that looked like this: public class myViewModel { public
I have a core data relationship that looks like this ItemA ->> ItemB where
I have a live database that had some data deleted from it and I
I had an old view that was giving out some odd data when I
I query a hierarchical data source like this: repository.LookupValue(IEnumerable<string> ascendants) where ascendants is a
Say I had the dataframe df <- data.frame('A' = c('a','a','a','a','b','b','b','b','b'), 'B' = c('y','y','z','z','y','y','y','z','z'), 'value'=c(1
Suppose I'm making a JDBC call and I had fetched data from a database
I Had Drop down list and I want to fill it with data from
Note: I had another similar question about how to GZIP data using Ruby's zlib
I had to convert some code (which deals with building a VBO of data

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.