我已从CSV导入数据,并希望使用共享值合并来自不同数据集的数据。
概述了一种方法here,但我正在努力让它自己运作
//要添加的新数据
var newdata = [{
'starting point': 'Berkner Island', 'color': '#0084B5',
'path': 'M 210.256,185.116'
}, {
'starting point': 'Hercules Inlet', 'color': '#E48428',
'path': 'M 156.355,241.624'
...
}];
//来自CSV的资源管理器数据
,,Name,First names,s,r,Nat,born,starting point,starting date,arrival date,days,km,Assist,Support,Style,note,
1,1,KAGGE,Erling,,,Nor,1/15/1963,Berkner Island,11/18/1992,1/7/1993,50,appr. 1300,n,n,solo,first solo unassisted,
2,2,ARNESEN,Liv,f,,Nor,6/1/1953,Hercules Inlet,11/4/1994,12/24/1994,50,1130,n,n,solo,first woman unassisted,
3,3,HAUGE,Odd Harald,,,Nor,1956,Berkner Island,11/4/1994,12/27/1994,54,appr. 1300,n,n,,,
//改编代码
d3.csv("explorer.csv", function(explorer) {
console.log(explorer);
});
explorer.forEach(function(explorer) {
var result = newdata.filter(function(newdat) {
return newdat['starting point'] === explorer.newdat['starting point'];
});
delete explorer.newdat['starting point'];
explorer.newdat = (result[0] !== undefined) ? result[0].name : null;
});
我做错了什么?
答案 0 :(得分:1)
explorer.foreach语句将抛出explorer is not defined
错误,因为它存在于d3.csv函数之外。
我假设您要添加起点匹配的颜色和路径。您需要将foreach添加到d3.csv函数中,如此。
d3.csv("explorer.csv", function(explorer) {
explorer.forEach(function(exp) {
var result = newdata.filter(function(newd) {
return exp['starting point'] === newd['starting point'];
});
exp.color = (result[0] !== undefined) ? result[0].color : null;
exp.path = (result[0] !== undefined) ? result[0].path : null;
});
console.log(explorer);
});