Data = [{s1: xx , s2: xx , … , s200: xx} ,
[{s1: xx , s2: xx , … , s200: xx} ,
….
[{s1: xx , s2: xx , … , s200: xx} ]
_.keys(data[0]) will get [“s1”,”s2”,…,”s200”]
totals = d3.nest().rollup(function(v){ return {
s1 : d3.sum(v, function(d){ return d.s1;}),
s2 : d3.sum(v, function(d){ return d.s2;}),
…..
s200 : d3.sum(v, function(d){ return d.s200;})
};}).entries(data);
如何通过循环键获得总数,即不逐一列出键?
答案 0 :(得分:0)
你可以这样做:
var keys = Object.keys(Data[0]); // save array of keys to variable
var totals = d3.nest().rollup(function(v){
var result = {};
keys.forEach(function(key) { // for each key
result[key] = d3.sum(v, function(d){ return d[key];}); // set appriptiate property with sum of this key
});
return result;
}).entries(Data);