let c=[
{
"eventid": 29096475,
"eventname": "Fortuna Dusseldorf v Stuttgart",
"opendate": "2019-02-09T18:30:00.000Z",
"eventtypeid": 1,
"eventtypename": "Soccer",
"marketname": "Match Odds",
"marketid": "1.154041432",
"runners": "[{\"selectionId\":879210,\"runnerName\":\"Fortuna Dusseldorf\",\"handicap\":0,\"sortPriority\":1},{\"selectionId\":44519,\"runnerName\":\"Stuttgart\",\"handicap\":0,\"sortPriority\":2},{\"selectionId\":58805,\"runnerName\":\"The Draw\",\"handicap\":0,\"sortPriority\":3}]",
"totalmatched": "7.18",
"selectionid": "879210",
"backoddprice": "[{\"price\":2.12,\"size\":234.19},{\"price\":2.02,\"size\":272.12},{\"price\":1.94,\"size\":251.02}]",
"layoddprice": "[{\"price\":2.36,\"size\":262.1},{\"price\":2.44,\"size\":15.95},{\"price\":2.58,\"size\":145.6}]"
},
{...}
]
let ui=[]
let j=c.reduce((o,a)=>{
let inner=ui.filter(h=>h.eventid==a.eventid);
if(inner.length>0)
{
a.runners.forEach(s=>{
if(a.selectionid==s.selectionId)
{
let ones={};
ones["selectionid"]=a.selectionid;
ones["selectionname"]=s.runnerName;
ones["backoddprice"]=a.backoddprice;
ones["layoddprice"]=a.layoddprice;
inner.runners.push(ones);
}
})
console.log("llllllllllll");
}
else{
let nn={};
let inrunners=[];
nn["eventid"]=a.eventid;
nn["eventname"]=a.eventname;
JSON.parse(a.runners).forEach(s=>{
if(a.selectionid==s.selectionId)
{
let ones={};
ones["selectionid"]=a.selectionid;
ones["selectionname"]=s.runnerName;
ones["backoddprice"]=a.backoddprice;
ones["layoddprice"]=a.layoddprice;
inrunners.push(ones);
}
})
nn["runners"]=inrunners;
o.push(nn);
}
return o;
},[])
console.log(JSON.stringify(j))
I have some data from the betfair api that requires sorting based on eventid and pushing runners array. I aim to achieve an output structure as specified below, where similar eventids are grouped together. Assistance in achieving this desired outcome would be greatly appreciated.
Expected format for each individual object
{
"eventid": 29065439,
"eventname": "Fortuna Dusseldorf v Stuttgart",
"opendate": "2019-02-01T18:30:00.000Z",
"eventtypeid": 1,
"eventtypename": "Soccer",
"marketname": "Match Odds",
"marketid": "1.153118681",
"runners": [
{
"selectionid": "879210",
"selectionname": "Fortuna Dusseldorf",
"backoddprice": "[{\"price\":2.12,\"size\":234.19},{\"price\":2.02,\"size\":272.12},{\"price\":1.94,\"size\":251.02}]",
"layoddprice": "[{\"price\":2.36,\"size\":262.1},{\"price\":2.44,\"size\":15.95},{\"price\":2.58,\"size\":145.6}]"
},
{
"selectionid": "44519",
"selectionname": "Stuttgart",
"backoddprice": "[{\"price\":3.3,\"size\":113.83},{\"price\":3.05,\"size\":180.24},{\"price\":2.9,\"size\":23.74}]",
"layoddprice": "[{\"price\":3.95,\"size\":262.48},{\"price\":4.6,\"size\":84.74},{\"price\":1000,\"size\":4.48}]"
},
{
"selectionid": "58805",
"selectionname": "The Draw",
"backoddprice": "[{\"price\":3.25,\"size\":116.58},{\"price\":3.05,\"size\":224.71},{\"price\":1.01,\"size\":139.27}]",
"layoddprice": "[{\"price\":3.95,\"size\":139.17},{\"price\":4.3,\"size\":122.84},{\"price\":4.5,\"size\":86.06}]"
}
]
}
My current attempts to solve this challenge has been constrained due to the size of the provided data. Any assistance in resolving this issue would be highly valued.