When working with highcharts, I need to generate parsed data to create series. The API data is structured like this:
[ date : XX,
series : [
player_id : 1,
team_id : 1,
score : 4 ],
[
player_id : 2,
team_id : 4,
score : 1 ],
[
player_id : 4,
team_id : 1,
score : 4 ]
],
[ date : XX+1
series : ...
],
...
Firstly, the user must select a team and players, but sometimes there may not be any results. For instance, when selecting 4 team IDs for date XX, the API might only return 2 of them, which needs to be taken into account. I’m trying to create different types of series - one for overall team scores, another for player scores, and more for individual team scores. This leads to multiple graphs being generated.
I am utilizing Ampersand.js, where the model remains constant but is called as many times as the number of teams selected in the form.
The function that I've implemented looks something like this:
function parse(data) {
var series = [];
var dates = [];
var findIndexByValue = function(arraytosearch, key, valuetosearch) {
for (var i = 0; i < arraytosearch.length; i++) {
if (arraytosearch[i][key] == valuetosearch) {
return i;
}
}
return null;
}
// Rest of the original code goes here...
}
While generating series, I encountered an issue when there are no results for a specific team in the API response.
An ideal array structure would resemble something like this:
series: [{
name: 'Team 1',
data: [4163, 5203, 6276, 5408, 3547, 3729, 3828,4163, 5203, 6276, 5408, 3547, 3729, 3828,4163, 5203, 6276, 5408, 3547, 3729, 3828]
},
{
name: 'Team 2',
data: [...]
}]
Furthermore, I noticed some redundancy in the code. Is there a clever way to simplify this using Array functions?
Thank you in advance, as this has been quite a challenge for me!