D3.js v4 sum values in an object

Given the following array of objects, we may want to sum values for a particular category. The category is gender in this case. So we want to be able to find the total value for all the ‘f’ and for the ‘m’ categories.
Great link and more indepth examples on Phoebe Bright‘s page.
Also a running example on github.

var data = [{
        name: "Sarah",
        gender: "f",
        value: 1
        name: "Bob",
        gender: "m",
        value: 2
        name: "Amy",
        gender: "f",
        value: 3

    var genderVals = d3.nest().key(function(d) {
        return d.gender;
      .rollup(function(leaves) {
        return d3.sum(leaves, function(d) {
          return d.value;
      .map(function(d) {
        return {
          Gender: d.key,
          totalValue: d.value

    genderVals.forEach(function(element) {

This will output the new array of objects called genderVals,

genderVals = [
{Gender: "f", totalValue: 4},
{Gender: "m", totalValue: 2}