我的elex.csv
采用此格式
"CONUM","PIXPMK","PIXMAR"
"1","461","436"
"2","398","428",
"3","447","436"
我想要这种格式的数据集 -
var dataset1 = [ [1, 417], [1, 436], [2, 398], [2, 428], [3, 447], [3, 436]];
我曾多次尝试过以这种形式从csv获取数据,但都是徒劳的。我附上了我的代码
var dataset1 = [];
d3.csv("elex.csv", function(data) {
dataset1 = data.map(function(d) { return [ [+d["CONUM"], +d["PIXMAR"]],[+d["CONUM"],+d["PIXPMK"]]]; });
console.log(dataset1)
});
这会将dataset1
作为
[[[1,417],[1,436]],[[2,398],[2,428]]]
答案 0 :(得分:3)
迭代每个元素,并将第一部分和第二部分分别推入dataset1
:
dataset1 = [];
d3.csv("elex.csv", function(data) {
data.forEach(function(d) {
dataset1.push([d['CONUM'], d['PIXMAR']]);
dataset1.push([d['CONUM'], d['PIXPMK']*100])
});
console.log(dataset1);
});