score:1

Accepted answer

Minor change:

Within the summary.map..., d.values would now be an array (due to the company key being used in the nest), and so you have to map this d.values internally just like you did in the previous attempt.

Here's the code change that'll make more sense:

 var summary = d3.nest()
    .key(function(d) {return d.leader})
    .key(function(d) {return d.company})
    .key(function(d) {return d.competent})
        .sortKeys(function(a, b) {return scaleValues.indexOf(a) - scaleValues.indexOf(b)})
    .rollup(function(l) {return l.length})
    .entries(data)
    .map(function(d) {
      return {
        key: d.key,
        values: d.values.map(function (row){ 
          return {
            key: row.key, 
            values: makeAllKeys(+d.key).map(function(k) {
              value = row.values.filter(function(v) {return v.key == k})[0]
              return value || ({key: k, values:0})
            })
          }
        })
    }
})

And yes, .map can be chained to the d3.nest itself.

Now the summary would look like the following:

[  
{  
  "key":"John",
  "values":[  
     {  
        "key":"A",
        "values":[  
           {  
              "key":"Strongly Disagree",
              "values":0
           },
           {  
              "key":"Disagree",
              "values":0
           },
           {  
              "key":"Neutral",
              "values":0
           },
           {  
              "key":"Agree",
              "value":2
           },
           {  
              "key":"Strongly Agree",
              "value":3
           }
        ]
     },
     {  }
  ]
},
{  
  "key":"Emily",
  "values":[  ]
 }
]

Here's a plunkr link for reference: http://plnkr.co/edit/Zz7dsTbmO8dI3n3SKkP3?p=preview

Hope this helps.


More Query from same tag