This inquiry has been presented in various formats, but I have yet to consolidate all the information in order to achieve the desired result. My apologies for reiterating what has already been addressed...
My objective is to transform an Array structure similar to this:
[
0: {
2014: "6",
2015: "19",
2016: "3",
2017: "12",
Name: "Jerry"
},
1: {
2014: "16",
2015: "9",
2016: "",
2017: "16",
Name: "Bill"
},
2: {
2014: "",
2015: "2",
2016: "43",
2017: "7",
Name: "Grace"
}
]
To a format resembling this:
{
"years": {
"2014": {
"Jerry": 6,
"Bill": 16,
"Grace": ""
},
"2015": {
"Jerry": 19,
"Bill": 9,
"Grace": 2
},
"2016": {
"Jerry": 3,
"Bill": "",
"Grace": 43
},
"2017": {
"Jerry": 12,
"Bill": 16,
"Grace": 7
}
}
}
Here is a snippet of code that remains after numerous attempts and modifications:
For your information, the data is sourced from a CSV file loaded through D3.js csv()
let loadData = () => {
return d3.csv(dataFile, function cb (err, d) {
if (err) throw err;
return d;
});
};
loadData().get((err, n) => {
if (err) throw err;
let years = [];
years.push(Object.keys(n[0]));
years = _.flatten(years);
years.splice(-1, 1);
console.log(n[0]);
n.map((x) => {
console.log(Object.keys(x[0]));
});
});