使用 map 时汇总 returns 未定义或具有值的数组

Rollup returns undefined or array with value when using map

我目前正在处理一个相当大的数据集,因此我希望通过将数据嵌套在新变量中来缩小数据变量 'nested_data'。

这是我正在使用的数据:

Landen,"Perioden","SITC","Onderwerpen_1","Waarde eenheid","Waarde"
Afghanistan,"2012","0 Voeding en levende dieren","Invoerwaarde","mln euro","1"
Afghanistan,"2012","1 Dranken en tabak","Invoerwaarde","mln euro","0"
Afghanistan,"2012","2 Grondstoffen niet eetbaar behalve...","Invoerwaarde","mln euro","."
Afghanistan,"2012","3 Minerale brandstoffen smeermiddele...","Invoerwaarde","mln euro","."
Afghanistan,"2012","4 Dierlijke en plantaardige oliën en ...","Invoerwaarde","mln euro","."
Afghanistan,"2012","5 Chemische producten","Invoerwaarde","mln euro","0"
Afghanistan,"2012","6 Fabricaten hoofdzakelijk gerangschi...","Invoerwaarde","mln euro","0"
Afghanistan,"2012","7 Machines en vervoermaterieel","Invoerwaarde","mln euro","1"
Afghanistan,"2012","8 Diverse gefabriceerde goederen","Invoerwaarde","mln euro","1"
Afghanistan,"2012","9 Niet afzonderlijk genoemde goederen","Invoerwaarde","mln euro","0"

我希望这是汇总的结果:

[
{
  "key": "0 Voeding en levende dieren",
  "value": 1
},
{
  "key": "1 Dranken en tabak",
  "value": 0
},
{
  "key": "2 Grondstoffen niet eetbaar behalve...",
  "value": 7
},
{
  "key": "3 Minerale brandstoffen smeermiddele...",
  "value": 1
},
{
  "key": "4 Dierlijke en plantaardige oliën en ...",
  "value": 9
},
{
  "key": "5 Chemische producten",
  "value": 4
},
{
  "key": "6 Fabricaten hoofdzakelijk gerangschi...",
  "value": 12
},
{
  "key": "7 Machines en vervoermaterieel",
  "value": 3
},
{
  "key": "8 Diverse gefabriceerde goederen",
  "value": 0
},
{
  "key": "9 Niet afzonderlijk genoemde goederen",
  "value": 13
}
]

我试过像这样使用 d3 汇总:

.rollup(function(d){
    return d.Waarde;
})

结果如下:

我的第二次尝试是使用'map',如下:

.rollup(function(value){
        return value.map(function(d){
            return d.Waarde;
        })
   })

我更喜欢这个结果,但它仍然不是我想要的:

虽然此方法确实向对象添加了正确的值,但它会将其放入一个数组中('map' 显然是这样做的)。但是我希望它不要将值放入数组中,而只是作为键的值。

在您的 rollup 方法中,第一个参数...

.rollup(function(d){
    return d.Waarde;
})

...不是一个对象:它是一个数组。因此,当您使用 map...

时,您几乎是正确的
.rollup(function(value){
    return value.map(function(d){
        return d.Waarde;
    })
})

...但问题是 map 将 return 一个数组,而这不是您想要的。你想要一个总和:

.rollup(function(d) {
    return d3.sum(d, function(e) {
        return e.Waarde
    });
})

这是一个对您的数据进行一些更改的演示(您将 "." 作为某些行中的值,这在这里没有什么意义):

const csv = `Landen,"Perioden","SITC","Onderwerpen_1","Waarde eenheid","Waarde"
Afghanistan,"2012","0 Voeding en levende dieren","Invoerwaarde","mln euro","1"
Afghanistan,"2012","1 Dranken en tabak","Invoerwaarde","mln euro","0"
Afghanistan,"2012","2 Grondstoffen niet eetbaar behalve...","Invoerwaarde","mln euro","4"
Afghanistan,"2012","3 Minerale brandstoffen smeermiddele...","Invoerwaarde","mln euro","2"
Afghanistan,"2012","4 Dierlijke en plantaardige oliën en ...","Invoerwaarde","mln euro","7"
Afghanistan,"2012","5 Chemische producten","Invoerwaarde","mln euro","0"
Afghanistan,"2012","6 Fabricaten hoofdzakelijk gerangschi...","Invoerwaarde","mln euro","0"
Afghanistan,"2012","7 Machines en vervoermaterieel","Invoerwaarde","mln euro","1"
Afghanistan,"2012","8 Diverse gefabriceerde goederen","Invoerwaarde","mln euro","1"
Afghanistan,"2012","9 Niet afzonderlijk genoemde goederen","Invoerwaarde","mln euro","0"
Afghanistan,"2012","9 Niet afzonderlijk genoemde goederen","Invoerwaarde","mln euro","5"
Afghanistan,"2012","9 Niet afzonderlijk genoemde goederen","Invoerwaarde","mln euro","2"
Afghanistan,"2012","9 Niet afzonderlijk genoemde goederen","Invoerwaarde","mln euro","9"`;

const data = d3.csvParse(csv, d3.autoType);

const nest = d3.nest()
  .key(function(d) {
    return d.SITC
  })
  .rollup(function(d) {
    return d3.sum(d, function(e) {
      return e.Waarde
    });
  })
  .entries(data)

console.log(nest)
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/5.7.0/d3.min.js"></script>