Pretty new to JS here and the array operations I'm used to in PHP obviously do not work in JS. I have an array of objects with a date (timestamp), a duration (number) and a mode (number):
{
"date": 111, //Not proper timestamp, just for the purpose of the question
"duration": 1.8333333333333333,
"mode": 2
},
{
"date": 111,
"duration": 3.4,
"mode": 1
},
{
"date": 111,
"duration": 2.9833333333333334,
"mode": 2
},
{
"date": 222,
"duration": 5.666666666666667,
"mode": 1
},
{
"date": 222,
"duration": 8.266666666666667,
"mode": 2
},
{
"date": 222,
"duration": 0.5666666666666667,
"mode": 1
},
{
"date": 333,
"duration": 9.25,
"mode": 2
}
]
What I'm looking for is to sum the durations of objects that share the same date and mode. ex:
{
"date": 111, //Not proper timestamp, just for the purpose of the question
"duration": 3.4,
"mode": 1
},
{
"date": 111,
"duration": 4.81,
"mode": 2
},
{
"date": 222,
"duration": 6.2,
"mode": 1
},
{
"date": 222,
"duration": 8.26,
"mode": 2
},
{
"date": 333,
"duration": 9.25,
"mode": 2
}
]
I have tried creating and manipulating 2 dimensional arrays without success.
For instance looping on my initial data set and updating a result_array like so:
result_array[mode][date]+=duration;
Thx