Reputation: 1683
I want to create a nested json file from a csv using d3.nest() however, I am having trouble getting the nesting right. My output looks like this:
Which is close but not quite- I would like the values to be key values: 0: etc 1: etc
Here is my code:
d3.csv("data/fifa-matches.csv", function(error, matchesCSV) {
//Loads in the tree information from fifa-tree.csv and calls createTree(csvData) to render the tree.
matchesCSV.forEach(function(d, i) {
this.teamData = d3.nest()
.key(function(d) {
return d.Team;
})
.rollup(function(d) {
return {
Result: { 'Label': d.Result, 'Rank': 'test' },
// total: d3.sum(v, function(d) { return d.amount; }),
// avg: d3.mean(v, function(d) { return d.amount; })
}
})
.entries(matchesCSV);
console.log(this.teamData);
});
d3.csv("data/fifa-tree.csv", function(error, treeCSV) {
//Create a unique "id" field for each game
treeCSV.forEach(function(d, i) {
//console.log(d);
d.id = d.Team + d.Opponent + i;
d.type = d.Type;
});
Any info in the right direction would be greatly appreciated!
Here is an example of the structure of the csv:
Team,Opponent,Goals Made,Goals Conceded,Delta Goals,Wins,Losses,Result Brazil,Germany,1,7,-6,0,1,Semi Finals Germany,Argentina,1,0,1,1,0,Winner Argentina,Netherlands,0,0,0,1,0,Semi Finals Netherlands,Brazil,3,0,3,1,0,Fourth Place Brazil,Colombia,2,1,1,1,0,Quarter Finals France,Germany,0,1,-1,0,1,Quarter Finals
Upvotes: 0
Views: 1558
Reputation: 2232
If you want to specify what's in each of the objects that correspond to a key, you can use the .rollup()
function
this.teamData = d3.nest()
.key(function(d) {
return d.Team;
}).rollup(function(match_object) {
return {
opponent: match_object[0]["Opponent"],
Goals Made: match_object[0]["Goals Made"],
..etc...
}).entries(matchesCSV);
and to return an array from treeCSV
function ReturnTeam(teamname){
for(team in treeCSV) {
if(teamname === team.Name) {
return team;
}
}
}
Upvotes: 2