I'm working on an Accounting System. I'm trying to display a bar chart with information about the Incomes and Expenses of every month. I get that information from a database via AJAX and I get the next JSON:
{
"results":{
"historic":[
{
"fecha":"2020-07-15",
"balance":"5433"
},
{
"fecha":"2020-07-15",
"balance":"6883.25"
},
{
"fecha":"2020-07-15",
"balance":"5383.15"
},
{
"fecha":"2020-07-18",
"balance":"12383.15"
},
{
"fecha":"2020-08-20",
"balance":"13385.35"
}
],
"monthly":{
"income":[
{
"month":"July\n",
"July\n":"5433"
},
{
"month":"July\n",
"July\n":"1450.25"
},
{
"month":"July\n",
"July\n":"0"
},
{
"month":"July\n",
"July\n":"7000.00"
},
{
"month":"August\n",
"August\n":"1002.20"
}
],
"expense":[
{
"month":"July\n",
"July\n":"0"
},
{
"month":"July\n",
"July\n":"0"
},
{
"month":"July\n",
"July\n":"1500.10"
},
{
"month":"July\n",
"July\n":"0"
},
{
"month":"August\n",
"August\n":"0"
}
]
}
}
}
The part under Historic is used for another chart that already works but the part under Monthly contains all the information that I need for the chart I'm currently working on.
THIS IS WHAT I NEED TO DO:
I need to create an array using Javascript that can contain the name of the month and the total Income or Expense of it. The problem is that I get one entry for every transaction on that month and I need to sum up. It's kinda difficult to explain it so I'm gonna give you an example.
I get this:
- July : $1000
- July : $300
- July : $50
- August : $400
- August : $7000
And I need to convert it to something like this inside of an array:
- July : $1350
- August : $7400
This is what I been working on but I think it is not the way to go:
//DRAW THE MONTHLY RESULTS GRAPHIC
var logs = [];
for(var i = 0; i < jsonObj['results']['historic'].length; i++) {
var results = jsonObj['results']['monthly'];
if(logs.some(logs => logs.month === results['income'][i].month)){
console.log("Object found inside the array.");
//Need a way to get the total of the month from the logs Array.
var newTotal = results['income'][i].month + oldTotal;
logs.month = newTotal;
} else{
console.log("Object not found.");
logs.push(results['income'][i]);
}
}
console.log(logs);
Thanks to everyone. I'm very new at this forum stuff and I hope I managed to explain it well.