简体   繁体   English

如何从D3.js中的csv文件创建嵌套对象的数组?

[英]How to create an array of nested objects from a csv file in D3.js?

This question uses the same data as one of my previous questions, but the question is different. 该问题与我以前的问题之一使用的数据相同,但是问题不同。 I have a csv file that looks like this: 我有一个csv文件,看起来像这样:

name,score,date
Bob,93,2014
Bob,85,2015
Barry,70,2015
...

No two people have the same name, but a person can have multiple entries. 没有两个人具有相同的名称,但是一个人可以具有多个条目。 How can I create an array within the d3.csv callback function that looks like this?: 如何在d3.csv回调函数中创建如下所示的数组?:

[{name: "Bob", values: [
   {score: 93, date: 2014},
   {score: 85, date: 2015}]}
 {name: "Barry", values: [
   {score: 70, date: 2015}]},
...

Normally I would be able to do this with plain javascript, but accessing property values with arrayName[objectIndex].objectPropertyName doesn't work within the d3 callback function. 通常,我可以使用普通的javascript做到这一点,但是使用arrayName[objectIndex].objectPropertyName访问属性值在d3回调函数中不起作用。

d3.nest() function that will convert your csv data into key value pairs. d3.nest()函数可将您的csv数据转换为键值对。 Look at this plnkr link to view the objects in console. 查看此plnkr链接以查看控制台中的对象。 To get minimum maximum data see this updated plnkr . 要获取最小的最大数据,请参阅此更新的plnkr

d3.csv("data.csv", function(error, data) {
    console.log(data);
    var updated = d3.nest().key(function(d) {
        return d.name;
    }).sortKeys(d3.ascending).entries(data);
    console.log(updated);
})

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM