Reputation: 319
My elex.csv
is in this format
"CONUM","PIXPMK","PIXMAR"
"1","461","436"
"2","398","428",
"3","447","436"
I want my dataset in this format-
var dataset1 = [ [1, 417], [1, 436], [2, 398], [2, 428], [3, 447], [3, 436]];
I have tried several times to get the fetch the data from csv in this form but all in vain. I am attaching my code
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)
});
this returns dataset1
as
[[[1,417],[1,436]],[[2,398],[2,428]]]
Upvotes: 1
Views: 76
Reputation: 12772
Iterate over each element, and push the first and second part into dataset1
separately:
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);
});
Upvotes: 3