Based on checkbox i check(used recoil for that) i got call from API with events List. I need to sort it with nesting because then i want to show it on my appilcation(names, with subnames etc, time etc). The thing is you can always show only 1 main sport(no need to showing names for tenis and football, only one). But i got problem with converting data to expected result
Call from API:
data:
[
{
"eventId": 30169172,
"remoteId": 35046955,
"eventName": "Polanka - Ssk Bilovec",
"eventStart": 1659279600000,
"eventType": 1,
"category3Id": 192,
"category2Id": 138,
"category1Id": 1,
"category3Name": "Puchar",
"category2Name": "Czechy",
"category1Name": "Piłka nożna",
"eventCodeId": null,
"gamesCount": 18,
"outcomesCount": null,
"treatAsSport": 1,
"category3AggregatedId": null,
"eventGames": [
{
"gameId": 750475832,
"gameName": "1X2",
"gameType": 1,
"gameCode": 0,
"argument": 0,
"combinationType": 1,
"marketTypes": [],
"gameLayout": 1,
"eventLayout": 1,
},
{
"gameId": 750511223,
"gameName": "Podwójna szansa",
"gameType": 4,
"gameCode": 0,
"argument": 0,
"combinationType": 1,
"marketTypes": [],
"gameLayout": 3,
"eventLayout": 14,
},
{
"gameId": 750511224,
"gameName": "Remis - nie ma zakładu (remis=zwrot)",
"gameType": 93,
"gameCode": 0,
"argument": 0,
"combinationType": 1,
"marketTypes": [],
"gameLayout": 2,
"eventLayout": 7,
},
{
"gameId": 750511234,
"gameName": "Obie drużyny strzelą bramkę",
"gameType": 98,
"gameCode": 0,
"argument": 0,
"combinationType": 1,
"marketTypes": [],
"gameLayout": 2,
"eventLayout": 6,
}
],
"cashoutEnabled": null,
"participants": [
{
"localId": 196702,
"remoteId": 390340,
"name": "Polanka",
"number": 1
},
{
"localId": 235004,
"remoteId": 426241,
"name": "Ssk Bilovec",
"number": 2
}
],
"tvChannelInfo": null,
"result": null,
"videoStream": null,
"betsCounter": null,
"sportId": null,
"countryId": null,
"leagueId": null,
"sportName": null,
"eventExtendedData": {
"neutralGround": "-1",
"remoteCategoryId": ""
},
"isOutright": null,
"marketTypesOrder": null,
"flashscoreLink": null,
"betBuilderModel": null
},
{
"eventId": 30169276,
"remoteId": 34859787,
"eventName": "Trutnov - Usti N Orlici",
"eventStart": 1659279600000,
"eventType": 1,
"category3Id": 192,
"category2Id": 138,
"category1Id": 1,
"category3Name": "Puchar",
"category2Name": "Czechy",
"category1Name": "Piłka nożna",
"eventCodeId": null,
"gamesCount": 19,
"outcomesCount": null,
"treatAsSport": 1,
"category3AggregatedId": null,
"eventGames": [
{
"gameId": 750479980,
"gameName": "1X2",
"gameType": 1,
"gameCode": 0,
"argument": 0,
"combinationType": 1,
"marketTypes": [],
"gameLayout": 1,
"eventLayout": 1,
},
{
"gameId": 750501665,
"gameName": "Podwójna szansa",
"gameType": 4,
"gameCode": 0,
"argument": 0,
"combinationType": 1,
"marketTypes": [],
"gameLayout": 3,
"eventLayout": 14,
},
{
"gameId": 750501663,
"gameName": "Remis - nie ma zakładu (remis=zwrot)",
"gameType": 93,
"gameCode": 0,
"argument": 0,
"combinationType": 1,
"marketTypes": [],
"gameLayout": 2,
"eventLayout": 7,
},
{
"gameId": 750501668,
"gameName": "Obie drużyny strzelą bramkę",
"gameType": 98,
"gameCode": 0,
"argument": 0,
"combinationType": 1,
"marketTypes": [],
"gameLayout": 2,
"eventLayout": 6,
}
],
"cashoutEnabled": null,
"participants": [
{
"localId": 83358,
"remoteId": 43657,
"name": "Trutnov",
"number": 1
},
{
"localId": 84918,
"remoteId": 35252,
"name": "Usti N Orlici",
"number": 2
}
],
"tvChannelInfo": null,
"result": null,
"videoStream": null,
"betsCounter": null,
"sportId": null,
"countryId": null,
"leagueId": null,
"sportName": null,
"eventExtendedData": {
"neutralGround": "-1",
"remoteCategoryId": ""
},
"isOutright": null,
"marketTypesOrder": null,
"flashscoreLink": null,
"betBuilderModel": null
},
{
"eventId": 30169445,
"remoteId": 34859607,
"eventName": "TJ Sokol Lanzhot - FK Hodonin",
"eventStart": 1659279600000,
"eventType": 1,
"category3Id": 192,
"category2Id": 138,
"category1Id": 1,
"category3Name": "Puchar",
"category2Name": "Czechy",
"category1Name": "Piłka nożna",
"eventCodeId": null,
"gamesCount": 19,
"outcomesCount": null,
"treatAsSport": 1,
"category3AggregatedId": null,
"eventGames": [
{
"gameId": 750485133,
"gameName": "1X2",
"gameType": 1,
"gameCode": 0,
"argument": 0,
"combinationType": 1,
"marketTypes": [],
"gameLayout": 1,
"eventLayout": 1,
},
{
"gameId": 750502968,
"gameName": "Podwójna szansa",
"gameType": 4,
"gameCode": 0,
"argument": 0,
"combinationType": 1,
"marketTypes": [],
"gameLayout": 3,
"eventLayout": 14,
},
{
"gameId": 750502969,
"gameName": "Remis - nie ma zakładu (remis=zwrot)",
"gameType": 93,
"gameCode": 0,
"argument": 0,
"combinationType": 1,
"marketTypes": [],
"gameLayout": 2,
"eventLayout": 7,
},
{
"gameId": 750502973,
"gameName": "Obie drużyny strzelą bramkę",
"gameType": 98,
"gameCode": 0,
"argument": 0,
"combinationType": 1,
"marketTypes": [],
"gameLayout": 2,
"eventLayout": 6,
}
],
"cashoutEnabled": null,
"participants": [
{
"localId": 138671,
"remoteId": 413745,
"name": "TJ Sokol Lanzhot",
"number": 1
},
{
"localId": 32927,
"remoteId": 53137,
"name": "FK Hodonin",
"number": 2
}
],
"tvChannelInfo": null,
"result": null,
"videoStream": null,
"betsCounter": null,
"sportId": null,
"countryId": null,
"leagueId": null,
"sportName": null,
"eventExtendedData": {
"neutralGround": "-1",
"remoteCategoryId": ""
},
"isOutright": null,
"marketTypesOrder": null,
"flashscoreLink": null,
"betBuilderModel": null
},
{
"eventId": 30181746,
"remoteId": 35060577,
"eventName": "Tj Tatran Bohunice - FK Apos Blansko",
"eventStart": 1659454200000,
"eventType": 1,
"category3Id": 192,
"category2Id": 138,
"category1Id": 1,
"category3Name": "Puchar",
"category2Name": "Czechy",
"category1Name": "Piłka nożna",
"eventCodeId": null,
"gamesCount": 1,
"outcomesCount": null,
"treatAsSport": 1,
"category3AggregatedId": null,
"eventGames": [
{
"gameId": 750896819,
"gameName": "1X2",
"gameType": 1,
"gameCode": 0,
"argument": 0,
"combinationType": 1,
"marketTypes": [],
"gameLayout": 1,
"eventLayout": 1,
}
],
"cashoutEnabled": null,
"participants": [
{
"localId": 232463,
"remoteId": 53129,
"name": "Tj Tatran Bohunice",
"number": 1
},
{
"localId": 35065,
"remoteId": 106331,
"name": "FK Apos Blansko",
"number": 2
}
],
"tvChannelInfo": null,
"result": null,
"videoStream": null,
"betsCounter": null,
"sportId": null,
"countryId": null,
"leagueId": null,
"sportName": null,
"eventExtendedData": {
"neutralGround": "-1",
"remoteCategoryId": ""
},
"isOutright": null,
"marketTypesOrder": null,
"flashscoreLink": null,
"betBuilderModel": null
},
{
"eventId": 29958729,
"remoteId": 34280159,
"eventName": "FK Varnsdorf - SFC Opawa",
"eventStart": 1659279600000,
"eventType": 1,
"category3Id": 966,
"category2Id": 138,
"category1Id": 1,
"category3Name": "2. Liga",
"category2Name": "Czechy",
"category1Name": "Piłka nożna",
"eventCodeId": null,
"gamesCount": 25,
"outcomesCount": null,
"treatAsSport": 1,
"category3AggregatedId": null,
"eventGames": [
{
"gameId": 743527938,
"gameName": "1X2",
"gameType": 1,
"gameCode": 0,
"argument": 0,
"combinationType": 1,
"marketTypes": [],
"gameLayout": 1,
"eventLayout": 1,
},
{
"gameId": 743527937,
"gameName": "Podwójna szansa",
"gameType": 4,
"gameCode": 0,
"argument": 0,
"combinationType": 1,
"marketTypes": [],
"gameLayout": 3,
"eventLayout": 14,
},
{
"gameId": 743527939,
"gameName": "Remis - nie ma zakładu (remis=zwrot)",
"gameType": 93,
"gameCode": 0,
"argument": 0,
"combinationType": 1,
"marketTypes": [],
"gameLayout": 2,
"eventLayout": 7,
},
{
"gameId": 747215198,
"gameName": "poniżej/powyżej 2.5 goli",
"gameType": 8,
"gameCode": 0,
"argument": 2.5,
"combinationType": 1,
"marketTypes": [],
"gameLayout": 2,
"eventLayout": 6,
},
{
"gameId": 747215302,
"gameName": "Obie drużyny strzelą bramkę",
"gameType": 98,
"gameCode": 0,
"argument": 0,
"combinationType": 1,
"marketTypes": [],
"gameLayout": 2,
"eventLayout": 6,
}
],
"cashoutEnabled": null,
"participants": [
{
"localId": 9797,
"remoteId": 34789,
"name": "FK Varnsdorf",
"number": 1
},
{
"localId": 9798,
"remoteId": 2215,
"name": "SFC Opawa",
"number": 2
}
],
"tvChannelInfo": null,
"result": null,
"videoStream": null,
"betsCounter": null,
"sportId": null,
"countryId": null,
"leagueId": null,
"sportName": null,
"eventExtendedData": {
"neutralGround": "-1",
"remoteCategoryId": ""
},
"isOutright": null,
"marketTypesOrder": null,
"flashscoreLink": null,
"betBuilderModel": null
},
{
"eventId": 30124046,
"remoteId": 34393125,
"eventName": "FC Dniepr Mogilev - FC Slavia Mozyr",
"eventStart": 1659286800000,
"eventType": 1,
"category3Id": 514,
"category2Id": 513,
"category1Id": 1,
"category3Name": "Puchar",
"category2Name": "Białoruś",
"category1Name": "Piłka nożna",
"eventCodeId": null,
"gamesCount": 25,
"outcomesCount": null,
"treatAsSport": 1,
"category3AggregatedId": null,
"eventGames": [
{
"gameId": 748950198,
"gameName": "1X2",
"gameType": 1,
"gameCode": 0,
"argument": 0,
"combinationType": 1,
"marketTypes": [],
"gameLayout": 1,
"eventLayout": 1,
},
{
"gameId": 749258553,
"gameName": "Podwójna szansa",
"gameType": 4,
"gameCode": 0,
"argument": 0,
"combinationType": 1,
"marketTypes": [],
"gameLayout": 3,
"eventLayout": 14,
},
{
"gameId": 749258574,
"gameName": "Remis - nie ma zakładu (remis=zwrot)",
"gameType": 93,
"gameCode": 0,
"argument": 0,
"combinationType": 1,
"marketTypes": [],
"gameLayout": 2,
"eventLayout": 7,
},
{
"gameId": 749258571,
"gameName": "poniżej/powyżej 2.5 goli",
"gameType": 8,
"gameCode": 0,
"argument": 2.5,
"combinationType": 1,
"marketTypes": [],
"gameLayout": 2,
"eventLayout": 6,
},
{
"gameId": 749258568,
"gameName": "Obie drużyny strzelą bramkę",
"gameType": 98,
"gameCode": 0,
"argument": 0,
"combinationType": 1,
"marketTypes": [],
"gameLayout": 2,
"eventLayout": 6,
}
],
"cashoutEnabled": null,
"participants": [
{
"localId": 3872,
"remoteId": 5231,
"name": "FC Dniepr Mogilev",
"number": 1
},
{
"localId": 6314,
"remoteId": 3358,
"name": "FC Slavia Mozyr",
"number": 2
}
],
"tvChannelInfo": null,
"result": null,
"videoStream": null,
"betsCounter": null,
"sportId": null,
"countryId": null,
"leagueId": null,
"sportName": null,
"eventExtendedData": {
"neutralGround": "-1",
"remoteCategoryId": ""
},
"isOutright": null,
"marketTypesOrder": null,
"flashscoreLink": null,
"betBuilderModel": null
}
]
I need to nest my data to look something like this:
[
{
sportName: Football,
countries:
{
Italy: { leagueName: [Serie A: and here all events from serie A , Serie B: etc ]},
Belgium: { leagueName: [First Division A: all events from First Division A ]
}
}]
It doesn't need to be exacly the same but looking for some concepts which can help to sort and nest it
Now my code is looking like this:
const transform = () => {
const sport = data[0].sportName
const league = data.reduce(
(array, element) => {
array[element.countries] ??= []
if (!array[element.countries].includes(element.leagueName)) {
array[element.countries].push(element.leagueName)
}
return array
},
{}
)
return { sport, league }
}
return transform()
and giving:
{ sportName: Football, countries: { Belgium: ['First Division A'], Italy: [Serie B, Serie A]}}