1
使用世界銀行的數據進行項目。他們的數據被組織起來,使他們在csv中有單獨的年份作爲列名。除了可能更改名稱之外,我將如何使用d3.csv訪問它們並將它們映射到自定義數組中,因爲我不會使用所有值。d3如果列名是數字,讀取csv/tsv文件?
例如,我使用的文件是每個國家的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將無法正常工作。如何顯示列名中的空格也如圖所示? 我會如何正確地在文檔本身或控制檯中顯示元素?
我很抱歉有這麼多問題。隨時指導我解決方案。謝謝。
感謝拉爾斯的回答。我要追加下一步我想做的事情。現在,如果我想使用這些數據創建線圖,我將如何訪問由此產生的對象數組?再次,我只想要200+元素陣列中的特定國家。
謝謝!一些很簡單的東西,我不知道。現在,如果我想使用這些數據創建線圖,我將如何訪問由此產生的對象數組?再次,我只想要200+元素陣列中的特定國家。 – kthieu
使用['.filter()'](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/filter)。 –