d3读取csv / tsv文件,如果列名是数字?

时间:2014-03-13 10:53:47

标签: javascript csv d3.js

使用世界银行的数据进行项目。他们的数据被组织起来,使得他们在csv中具有单独的年份作为列名。除了可能更改名称之外,我将如何使用d3.csv访问它们并将它们映射到自定义数组中,因为我不会使用所有值。

例如,我使用的文件是每个国家/地区的GDP。每个元素/行都是这样形成的

"Country Name","Country Code","Indicator Name","Indicator Code", "1960","1961","1962","1963","1964","1965","1966","1967","1968","1969","1970","1971","1972","1973","1974","1975","1976","1977","1978","1979","1980","1981","1982","1983","1984","1985","1986","1987","1988","1989","1990","1991","1992","1993","1994","1995","1996","1997","1998","1999","2000","2001","2002","2003","2004","2005","2006","2007","2008","2009","2010","2011","2012","2013",

如果我想要一个像英国,巴西,中国,俄罗斯,美国等国家的GDP值,以及2004 - 2012年,我该怎么办?

代码看起来像这样吗

d3.csv(URL, function(d) {
     return {
         AttributeName : d.ColumnName
         //Continues for all columns I need
     };

});

同样,如果列名是实际整数,则d.ColumnName不起作用。我如何计算列名中的空格如图所示? 如何在文档本身或控制台中正确显示元素?

我为这么多问题道歉。随意指导我解决方案。感谢。

感谢Lars的回答。我要追加下一步我要做的事情。现在,如果我想使用这些数据制作折线图,我将如何访问生成的对象数组?同样,我只想要200多个元素阵列中的特定国家。

1 个答案:

答案 0 :(得分:3)

您可以使用替代语法来访问属性来解决所有这些问题 - 而不是

d.foo

你可以写

d["foo"]

这也适用于空格,数字等:

d["11111"]
d["string with spaces"]