I want to change the structure of the current Array that I am consuming for a better use case, I want to group items into subarrays in this order. soccer > segment name > league > fixtures . I have been stuck on this for hours. I tried to use reduce but none of my attempts was close to success. Below is the sample data and the result I am trying to achieve.
const currentData = [
{
"fixtureName": "Fulham vs Chelsea",
"league": "Premier League",
"sport": "Soccer",
"segmentName": "England",
"markets": []
},
{
"fixtureName": "Arsenal vs liverpool",
"league": "Premier League",
"sport": "Soccer",
"segmentName": "England",
"markets": []
}, {
"fixtureName": "Middlesbrough FC vs. West Bromwich Albion",
"league": "Championship",
"sport": "Soccer",
"segmentName": "England",
"markets": []
}, {
"fixtureName": "Club El Porvenir vs. Berazategui",
"league": "Primera C",
"sport": "Soccer",
"segmentName": "Argentina",
"markets": []
}, {
"fixtureName": "Lille vs Angers ",
"league": "Ligue 1",
"sport": "Soccer",
"segmentName": "France",
"markets": []
}, {
"fixtureName": "PSG vs Nantes",
"league": "Ligue 1",
"sport": "Soccer",
"segmentName": "France",
"markets": []
}, {
"fixtureName": "Argentino de Quilmes vs. Comunicaciones",
"league": "Primera B",
"sport": "Soccer",
"segmentName": "Argentina",
"markets": []
}, {
"fixtureName": "Deportivo Merlo vs. Club Atletico Fenix",
"league": "Primera B",
"sport": "Soccer",
"segmentName": "Argentina",
"markets": []
},
]
This is the type of output and result I want
const data = [
{
sportID: 1,
sportName: 'Soccer',
categories: [
{
itemName: 'Argentina',
tournaments: [
{
itemID: 1,
itemName: 'ARG - Primera B',
events: [
{
id: 1,
itemName: 'Argentino de Quilmes vs. Comunicaciones',
markets: [],
},
{
id: 2,
itemName: 'Deportivo Merlo vs. Club Atletico Fenix',
markets: [],
},
],
},
{
itemID: 2,
itemName: 'ARG - Primera C',
events: [
{
id: 1,
itemName: 'Club El Porvenir vs. Berazategui',
markets: [],
},
],
},
],
},
{
itemName: 'England',
tournaments: [
{
itemID: 1,
itemName: 'ENG - Premier League',
events: [
{
id: 1,
itemName: 'Arsenal vs Liverpool',
markets: [],
},
{
id: 2,
itemName: 'Fulham vs Chelsea',
markets: [],
},
],
},
{
itemID: 2,
itemName: 'ENG - Championship',
events: [
{
id: 1,
itemName: 'Middlesbrough FC vs. West Bromwich Albion',
markets: [],
},
],
},
],
},
{
itemName: 'France',
tournaments: [
{
itemID: 1,
itemName: 'Ligue 1',
events: [
{
id: 1,
itemName: 'PSG vs Nantes',
markets: [],
},
{
id: 2,
itemName: 'Lille vs Angers',
markets: [],
},
],
},
],
},
],
},
];
CodePudding user response:
The expected results don't quite match the input, but here's a way to reduce
each item to its location in the tree, by first creating the tree's objects if not already created.
var currentData = [{"fixtureName":"Fulham vs Chelsea","league":"Premier League","sport":"Soccer","segmentName":"England","markets":[]},{"fixtureName":"Arsenal vs liverpool","league":"Premier League","sport":"Soccer","segmentName":"England","markets":[]},{"fixtureName":"Middlesbrough FC vs. West Bromwich Albion","league":"Championship","sport":"Soccer","segmentName":"England","markets":[]},{"fixtureName":"Club El Porvenir vs. Berazategui","league":"Primera C","sport":"Soccer","segmentName":"Argentina","markets":[]},{"fixtureName":"Lille vs Angers ","league":"Ligue 1","sport":"Soccer","segmentName":"France","markets":[]},{"fixtureName":"PSG vs Nantes","league":"Ligue 1","sport":"Soccer","segmentName":"France","markets":[]},{"fixtureName":"Argentino de Quilmes vs. Comunicaciones","league":"Primera B","sport":"Soccer","segmentName":"Argentina","markets":[]},{"fixtureName":"Deportivo Merlo vs. Club Atletico Fenix","league":"Primera B","sport":"Soccer","segmentName":"Argentina","markets":[]}]
var obj = currentData.reduce(function(agg, item) {
agg[item.sport] ??= {}
agg[item.sport][item.segmentName] ??= {}
agg[item.sport][item.segmentName][item.league] ??= {}
agg[item.sport][item.segmentName][item.league][item.fixtureName] ??= []
agg[item.sport][item.segmentName][item.league][item.fixtureName].push(item)
return agg;
}, {});
console.log(obj)
.as-console-wrapper {
max-height: 100% !important;
}