I am trying to prepare data to use with the amCharts library. The final data structure needs to be formatted like this:
data = [{
name: "First",
total: 190,
children: [
{
name: "A1",
value: 100
},
{
name: "A2",
value: 60
},
{
name: "A3",
value: 30
},
],
name: "Second",
total: 150,
children: [
{
name: "B1",
value: 100
},
{
name: "B2",
value: 30
},
{
name: "B3",
value: 20
},
]
}]
My prepped data looks like this:
const data = {
'Corporate Investors': {
total: 7, // total number of 'Corporate Investors', not the sum total of 'stages'
stages: {
Seed: 7,
'Series A': 7,
'Series B': 7,
'Series C': 7,
'Series D': 7
}
},
'Angel investor': {
total: 11,
stages: {
Seed: 10,
'Series A': 10,
'Series B': 11,
'Series C': 11,
'Series D': 9
}...
I've made a helper function to turn this object into the type of array that amCharts needs (modifying one from one of their demos found here: https://www.amcharts.com/demos/drill-down-treemap/
function processData(data) {
const treeData = [];
let typeData = {};
// loops through the outer 'Angel investor' type-keys
for (const key of Object.keys(data)) {
typeData = { name: key, total: data[key].total, children: [] };
// loops over the 'stages' for each of the outer keys
for (const stage of Object.keys(data[key].stages)) {
typeData.children.push({ name: stage, count: data[key].stages[stage] });
}
console.log('typeData pre-push: ', typeData);
console.log('treeData pre-push: ', treeData);
treeData.push(typeData);
console.log('treeData post-push: ', treeData);
}
return treeData;
}
There are couple of nested loops and it might be hard to follow but it DOES successfully take my original object and process it into a array format that amCharts is after, except when it is PUSHED into treeData I seem to lose the CHILDREN values. Here is the output of the console.logs from VS Code:
I have confirmed this by pulling in the data into my react component and logging it there and the children array is completely empty.
What is happening to my data when I push it in that is causing it to lose all of my children values?

processDataworktypeDatainside theforloop