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

  • Home
  • SEARCH
  • 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 8935807
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T10:04:39+00:00 2026-06-15T10:04:39+00:00

I recently posted this question about summing arrays in JavaScript using d3.nest() I got

  • 0

I recently posted this question about summing arrays in JavaScript using d3.nest()

I got a good solution (two in fact), but it turns out both have an issue when adapted to append additional information:

data = [
  {
    continent: 'africa',
    country: 'gabon',
    values: [1, 2]
  }, {
    continent: 'africa',
    country: 'chad',
    values: [3, 4]
  }, {
    continent: 'asia',
    country: 'china',
    values: [1, 2]
  }
];

sum_by = 'continent';

rollupAgg = function(data, sum_by) {
  return d3.nest().key(function(d) {
    return d[sum_by];
  }).rollup(function(group) {
    return group.reduce(function(prev, cur, index, arr) {
      return {
        values: prev.values.map(function(d, i) {
          return d + cur.values[i];
        }),
        sum_by: sum_by       // additional information
      };
    });
  }).entries(data);
};

reduce() doesn’t run if there is only one row in that group, so this returns the following:

[
  {
    "key": "africa",
    "values": {
      "values": [4, 6],
      "sum_by": "continent"
    }
  }, {
    "key": "asia",
    "values": {
      "continent": "asia",
      "country": "china",   // country information shouldn't have been retained
      "values": [1, 2]
    }                       // sum_by information should have been added
  }
];

Can you see a way to modify this function so that it returns the desired result?

  • 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-15T10:04:40+00:00Added an answer on June 15, 2026 at 10:04 am

    Hasn’t occurred to me till now that a single element array will not execute the function; but it makes sense, because you’re using with a single param:

    [].reduce(function(prev, curr, i, arr) {});// <-- function is the only param
    

    When used with a single param, the behavior is that the first time the function is executed, i equals 1 (not 0), and prev and curr are the first and second elements of the array. Since you only have one element, there’s no way to call it in this form.

    If you use reduce with a 2nd param:

    [].reduce(function(prev, curr, i, arr) {}, { foo:'bar' });// <-- 2nd param {foo:bar}
    

    it does actually call the function, with the first call passing i equal to 0 and prev equaling { foo:'bar' }.

    So I guess you have 2 options:

    1. Either modify it to pass a 2nd param, which in your case would need to be { values:[0,0] } (and that hardcodes the fact that values is always 2 elements, which will cause an issue if it’s longer).

    2. Check if group.length == 1 and if so, return group, instead of calling reduce.

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

Sidebar

Related Questions

I recently posted this question on the r-help mailing list but got no answers,
Recently I posted a question about the html helper dropdownlist and got it working
I recently posted this question about codes for a gift-card-like voucher that users can
Recently this question was posted about the definition of what a transaction is in
I recently posted a question about connecting two queries to omit elements found in
As demonstrated in this answer I recently posted, I seem to be confused about
I recently posted a question about screen rotation in my live wallpaper. To test
I recently posted a question about centering a page with CSS. I figured out
I recently posted a question using a lambda function and in a reply someone
I recently posted a question about getting a NullPointerException whenever I called an array

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.