简体   繁体   English

使用d3.js读取csv文件时出错

[英]error while reading csv file using d3.js

I am new to d3.js any help is much appreciated csv file is 我是d3.js的新手,非常感谢csv文件的帮助

Year,Make,Model,Length
1997,Ford,E350,2.34
2000,Mercury,Cougar,2.38

code is 代码是

d3.csv("../test/car.csv", function(d) {
  return {
    year: new Date(+d.Year, 0, 1), // convert "Year" column to Date
    make: d.Make,
    model: d.Model,
    length: +d.Length // convert "Length" column to number
  };
}, function(error, rows) {
  console.log(rows);
});

expected result is 预期结果是

[
  {"Year": "1997", "Make": "Ford", "Model": "E350", "Length": "2.34"},
  {"Year": "2000", "Make": "Mercury", "Model": "Cougar", "Length": "2.38"}
]

but i am getting the following error Uncaught SyntaxError: Unexpected token ILLEGAL 但我收到以下错误未捕获的SyntaxError:意外的令牌ILLEGAL

also shows that the length as 86 but i have only 2 entries in the csv file not sure what is happening 还显示长度为86,但我在csv文件中只有2个条目,不确定发生了什么

I guess that musically_ut comment will solve your primary issue. 我想,musicly_ut评论会解决您的主要问题。 However you also don't iterate on your data. 但是,您也不会迭代数据。


The data in d is an array with every entry being a row. d的数据是一个数组,每个条目都是一行。

Hence, if you did: 因此,如果您这样做:

d3.csv("../test/car.csv", function(d) {
  return d;
}, function(error, rows) {
  console.log(rows);
});

I guess that you would have the desired result. 我想您会得到理想的结果。

If you want to go through your data you can do the following: 如果要查看数据,可以执行以下操作:

d3.csv("../test/car.csv", function(d) {
  d.forEach(function(data,index){
    // do something with row of index `index` with its data `data`
  })
}, function(error, rows) {
  console.log(rows);
});

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

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