In my JavaScript code, I have an array that looks like this:
var data = [
{ y: '2017-01', a: 50, b: 90, c:110},
{ y: '2017-02', a: 65, b: 75, c:120},
{ y: '2017-03', a: 50, b: 50, c:10},
{ y: '2017-04', a: 75, b: 60, c:170},
{ y: '2017-05', a: 80, b: 65, c:60},
{ y: '2017-06', a: 90, b: 70, c:30},
{ y: '2017-07', a: 100, b: 75, c:10},
{ y: '2017-08', a: 115, b: 75, c:0},
{ y: '2017-09', a: 120, b: 85, c:0}
]
I've set up an ajax call to retrieve JSON data.
$.ajax({
type: "POST",
url: "php/get_incomes_months.php",
data: {year:current_year},
dataType: "json",
success: function(data) {
var data_array = [
{y:data[0]['date'], a:data[0]['validated'], b:data[0]['revenue'], c:data[0]['costes']},
{y:data[1]['date'], a:data[1]['validated'], b:data[1]['revenue'], c:data[1]['costes']}
];
fooDone(data_array); //after we have data, we pass it to fooDone
},
error: function(data){
}
});
The JSON response is structured as follows:
0 Object
validated "0"
revenue "50244.81"
costes "0"
date "2017-01"
1 Object
validated "44788.16"
revenue "30640.51"
costes "0"
date "2017-02"
2 Object
validated "68324.64"
revenue "80363.51"
costes "11072.73"
date "2017-03"
3 Object
validated "0"
revenue "50244.81"
costes "0"
date "2017-04"
4 Object
validated "2738"
revenue "103145.62"
costes "11"
date "2017-05"
5 Object
validated "510"
revenue "1459283.09"
costes "213"
date "2017-06"
6 Object
validated "24034.21"
revenue "50367.81"
costes "0"
date "2017-07"
7 Object
validated "24"
revenue "50244.81"
costes "0"
date "2017-08"
As you can see in the success function, I am currently manually creating the array objects. Any suggestions on how I can improve this process with a FOR loop?
success: function(data) {
var data_array = [
{y:data[0]['date'], a:data[0]['validated'], b:data[0]['revenue'], c:data[0]['costes']},
{y:data[1]['date'], a:data[1]['validated'], b:data[1]['revenue'], c:data[1]['costes']}
];
Your assistance would be greatly appreciated.