gpt4 book ai didi

javascript - 如何减少json数据

转载 作者:行者123 更新时间:2023-12-01 01:18:51 25 4
gpt4 key购买 nike

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}]"
},
{
"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": "44519",
"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}]"
},
{
"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": "58805",
"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}]"
},
{
"eventid": 29065439,
"eventname": "Hannover v RB Leipzig",
"opendate": "2019-02-01T18:30:00.000Z",
"eventtypeid": 1,
"eventtypename": "Soccer",
"marketname": "Match Odds",
"marketid": "1.153118681",
"runners": "[{\"selectionId\":197307,\"runnerName\":\"Hannover\",\"handicap\":0,\"sortPriority\":1},{\"selectionId\":5340398,\"runnerName\":\"RB Leipzig\",\"handicap\":0,\"sortPriority\":2},{\"selectionId\":58805,\"runnerName\":\"The Draw\",\"handicap\":0,\"sortPriority\":3}]",
"totalmatched": "340.79",
"selectionid": "197307",
"backoddprice": "[{\"price\":6,\"size\":206.45},{\"price\":5.8,\"size\":46.89},{\"price\":5.7,\"size\":216.99}]",
"layoddprice": "[{\"price\":6.4,\"size\":38.75},{\"price\":6.6,\"size\":275.14},{\"price\":7,\"size\":142.04}]"
},
{
"eventid": 29065439,
"eventname": "Hannover v RB Leipzig",
"opendate": "2019-02-01T18:30:00.000Z",
"eventtypeid": 1,
"eventtypename": "Soccer",
"marketname": "Match Odds",
"marketid": "1.153118681",
"runners": "[{\"selectionId\":197307,\"runnerName\":\"Hannover\",\"handicap\":0,\"sortPriority\":1},{\"selectionId\":5340398,\"runnerName\":\"RB Leipzig\",\"handicap\":0,\"sortPriority\":2},{\"selectionId\":58805,\"runnerName\":\"The Draw\",\"handicap\":0,\"sortPriority\":3}]",
"totalmatched": "340.79",
"selectionid": "5340398",
"backoddprice": "[{\"price\":1.58,\"size\":523.32},{\"price\":1.57,\"size\":319.63},{\"price\":1.56,\"size\":1755.76}]",
"layoddprice": "[{\"price\":1.61,\"size\":180.92},{\"price\":1.62,\"size\":944.98},{\"price\":1.64,\"size\":791.3}]"
},
{
"eventid": 29065439,
"eventname": "Hannover v RB Leipzig",
"opendate": "2019-02-01T18:30:00.000Z",
"eventtypeid": 1,
"eventtypename": "Soccer",
"marketname": "Match Odds",
"marketid": "1.153118681",
"runners": "[{\"selectionId\":197307,\"runnerName\":\"Hannover\",\"handicap\":0,\"sortPriority\":1},{\"selectionId\":5340398,\"runnerName\":\"RB Leipzig\",\"handicap\":0,\"sortPriority\":2},{\"selectionId\":58805,\"runnerName\":\"The Draw\",\"handicap\":0,\"sortPriority\":3}]",
"totalmatched": "340.79",
"selectionid": "58805",
"backoddprice": "[{\"price\":4.5,\"size\":170.07},{\"price\":4.4,\"size\":151.38},{\"price\":4.3,\"size\":465.06}]",
"layoddprice": "[{\"price\":4.8,\"size\":278.55},{\"price\":4.9,\"size\":89.63},{\"price\":5,\"size\":166.17}]"
},
{
"eventid": 29096467,
"eventname": "Mgladbach v Hertha Berlin",
"opendate": "2019-02-08T18:30:00.000Z",
"eventtypeid": 1,
"eventtypename": "Soccer",
"marketname": "Match Odds",
"marketid": "1.154040688",
"runners": "[{\"selectionId\":84649,\"runnerName\":\"Mgladbach\",\"handicap\":0,\"sortPriority\":1},{\"selectionId\":44518,\"runnerName\":\"Hertha Berlin\",\"handicap\":0,\"sortPriority\":2},{\"selectionId\":58805,\"runnerName\":\"The Draw\",\"handicap\":0,\"sortPriority\":3}]",
"totalmatched": "55.78",
"selectionid": "84649",
"backoddprice": "[{\"price\":1.78,\"size\":13.31},{\"price\":1.76,\"size\":235.77},{\"price\":1.69,\"size\":15.54}]",
"layoddprice": "[{\"price\":1.85,\"size\":262.21},{\"price\":1.86,\"size\":13.39},{\"price\":2.1,\"size\":163.36}]"
},
{
"eventid": 29096467,
"eventname": "Mgladbach v Hertha Berlin",
"opendate": "2019-02-08T18:30:00.000Z",
"eventtypeid": 1,
"eventtypename": "Soccer",
"marketname": "Match Odds",
"marketid": "1.154040688",
"runners": "[{\"selectionId\":84649,\"runnerName\":\"Mgladbach\",\"handicap\":0,\"sortPriority\":1},{\"selectionId\":44518,\"runnerName\":\"Hertha Berlin\",\"handicap\":0,\"sortPriority\":2},{\"selectionId\":58805,\"runnerName\":\"The Draw\",\"handicap\":0,\"sortPriority\":3}]",
"totalmatched": "55.78",
"selectionid": "44518",
"backoddprice": "[{\"price\":4.2,\"size\":83.73},{\"price\":3.95,\"size\":120.42},{\"price\":1.04,\"size\":412.45}]",
"layoddprice": "[{\"price\":5.2,\"size\":94.84},{\"price\":1000,\"size\":6.14}]"
},
{
"eventid": 29096467,
"eventname": "Mgladbach v Hertha Berlin",
"opendate": "2019-02-08T18:30:00.000Z",
"eventtypeid": 1,
"eventtypename": "Soccer",
"marketname": "Match Odds",
"marketid": "1.154040688",
"runners": "[{\"selectionId\":84649,\"runnerName\":\"Mgladbach\",\"handicap\":0,\"sortPriority\":1},{\"selectionId\":44518,\"runnerName\":\"Hertha Berlin\",\"handicap\":0,\"sortPriority\":2},{\"selectionId\":58805,\"runnerName\":\"The Draw\",\"handicap\":0,\"sortPriority\":3}]",
"totalmatched": "55.78",
"selectionid": "58805",
"backoddprice": "[{\"price\":3.75,\"size\":17.08},{\"price\":3.7,\"size\":115.18},{\"price\":3.55,\"size\":102.56}]",
"layoddprice": "[{\"price\":4.8,\"size\":264.34},{\"price\":1000,\"size\":11.1}]"
}


]
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))

我有这个 betfair api 数据,我想对其进行排序,我正在寻找的是,如果 eventid 相同,则应推送运行者数组中的数据,我希望预期输出应如下所示,我已经尝试过,但我我无法达到预期的结果。任何帮助将不胜感激。谢谢

I expect something like this 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}]"
}
]
}

这是我到目前为止所尝试过的,数据相当大,所以我无法在这里创建片段

最佳答案

我无法将您的输入 json 和预期输出 json 关联起来。

这里是测试的jsfiddle链接https://jsfiddle.net/4tv0dsw6/

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}]"
},
{
"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": "44519",
"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}]"
},
{
"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": "58805",
"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}]"
},
{
"eventid": 29065439,
"eventname": "Hannover v RB Leipzig",
"opendate": "2019-02-01T18:30:00.000Z",
"eventtypeid": 1,
"eventtypename": "Soccer",
"marketname": "Match Odds",
"marketid": "1.153118681",
"runners": "[{\"selectionId\":197307,\"runnerName\":\"Hannover\",\"handicap\":0,\"sortPriority\":1},{\"selectionId\":5340398,\"runnerName\":\"RB Leipzig\",\"handicap\":0,\"sortPriority\":2},{\"selectionId\":58805,\"runnerName\":\"The Draw\",\"handicap\":0,\"sortPriority\":3}]",
"totalmatched": "340.79",
"selectionid": "197307",
"backoddprice": "[{\"price\":6,\"size\":206.45},{\"price\":5.8,\"size\":46.89},{\"price\":5.7,\"size\":216.99}]",
"layoddprice": "[{\"price\":6.4,\"size\":38.75},{\"price\":6.6,\"size\":275.14},{\"price\":7,\"size\":142.04}]"
},
{
"eventid": 29065439,
"eventname": "Hannover v RB Leipzig",
"opendate": "2019-02-01T18:30:00.000Z",
"eventtypeid": 1,
"eventtypename": "Soccer",
"marketname": "Match Odds",
"marketid": "1.153118681",
"runners": "[{\"selectionId\":197307,\"runnerName\":\"Hannover\",\"handicap\":0,\"sortPriority\":1},{\"selectionId\":5340398,\"runnerName\":\"RB Leipzig\",\"handicap\":0,\"sortPriority\":2},{\"selectionId\":58805,\"runnerName\":\"The Draw\",\"handicap\":0,\"sortPriority\":3}]",
"totalmatched": "340.79",
"selectionid": "5340398",
"backoddprice": "[{\"price\":1.58,\"size\":523.32},{\"price\":1.57,\"size\":319.63},{\"price\":1.56,\"size\":1755.76}]",
"layoddprice": "[{\"price\":1.61,\"size\":180.92},{\"price\":1.62,\"size\":944.98},{\"price\":1.64,\"size\":791.3}]"
},
{
"eventid": 29065439,
"eventname": "Hannover v RB Leipzig",
"opendate": "2019-02-01T18:30:00.000Z",
"eventtypeid": 1,
"eventtypename": "Soccer",
"marketname": "Match Odds",
"marketid": "1.153118681",
"runners": "[{\"selectionId\":197307,\"runnerName\":\"Hannover\",\"handicap\":0,\"sortPriority\":1},{\"selectionId\":5340398,\"runnerName\":\"RB Leipzig\",\"handicap\":0,\"sortPriority\":2},{\"selectionId\":58805,\"runnerName\":\"The Draw\",\"handicap\":0,\"sortPriority\":3}]",
"totalmatched": "340.79",
"selectionid": "58805",
"backoddprice": "[{\"price\":4.5,\"size\":170.07},{\"price\":4.4,\"size\":151.38},{\"price\":4.3,\"size\":465.06}]",
"layoddprice": "[{\"price\":4.8,\"size\":278.55},{\"price\":4.9,\"size\":89.63},{\"price\":5,\"size\":166.17}]"
},
{
"eventid": 29096467,
"eventname": "Mgladbach v Hertha Berlin",
"opendate": "2019-02-08T18:30:00.000Z",
"eventtypeid": 1,
"eventtypename": "Soccer",
"marketname": "Match Odds",
"marketid": "1.154040688",
"runners": "[{\"selectionId\":84649,\"runnerName\":\"Mgladbach\",\"handicap\":0,\"sortPriority\":1},{\"selectionId\":44518,\"runnerName\":\"Hertha Berlin\",\"handicap\":0,\"sortPriority\":2},{\"selectionId\":58805,\"runnerName\":\"The Draw\",\"handicap\":0,\"sortPriority\":3}]",
"totalmatched": "55.78",
"selectionid": "84649",
"backoddprice": "[{\"price\":1.78,\"size\":13.31},{\"price\":1.76,\"size\":235.77},{\"price\":1.69,\"size\":15.54}]",
"layoddprice": "[{\"price\":1.85,\"size\":262.21},{\"price\":1.86,\"size\":13.39},{\"price\":2.1,\"size\":163.36}]"
},
{
"eventid": 29096467,
"eventname": "Mgladbach v Hertha Berlin",
"opendate": "2019-02-08T18:30:00.000Z",
"eventtypeid": 1,
"eventtypename": "Soccer",
"marketname": "Match Odds",
"marketid": "1.154040688",
"runners": "[{\"selectionId\":84649,\"runnerName\":\"Mgladbach\",\"handicap\":0,\"sortPriority\":1},{\"selectionId\":44518,\"runnerName\":\"Hertha Berlin\",\"handicap\":0,\"sortPriority\":2},{\"selectionId\":58805,\"runnerName\":\"The Draw\",\"handicap\":0,\"sortPriority\":3}]",
"totalmatched": "55.78",
"selectionid": "44518",
"backoddprice": "[{\"price\":4.2,\"size\":83.73},{\"price\":3.95,\"size\":120.42},{\"price\":1.04,\"size\":412.45}]",
"layoddprice": "[{\"price\":5.2,\"size\":94.84},{\"price\":1000,\"size\":6.14}]"
},
{
"eventid": 29096467,
"eventname": "Mgladbach v Hertha Berlin",
"opendate": "2019-02-08T18:30:00.000Z",
"eventtypeid": 1,
"eventtypename": "Soccer",
"marketname": "Match Odds",
"marketid": "1.154040688",
"runners": "[{\"selectionId\":84649,\"runnerName\":\"Mgladbach\",\"handicap\":0,\"sortPriority\":1},{\"selectionId\":44518,\"runnerName\":\"Hertha Berlin\",\"handicap\":0,\"sortPriority\":2},{\"selectionId\":58805,\"runnerName\":\"The Draw\",\"handicap\":0,\"sortPriority\":3}]",
"totalmatched": "55.78",
"selectionid": "58805",
"backoddprice": "[{\"price\":3.75,\"size\":17.08},{\"price\":3.7,\"size\":115.18},{\"price\":3.55,\"size\":102.56}]",
"layoddprice": "[{\"price\":4.8,\"size\":264.34},{\"price\":1000,\"size\":11.1}]"
}


];
console.log("c",c)

//Code starts from here
result = [];
mappedEventIds = [];
a = c.reduce((v, k)=>{
k.runners = JSON.parse(k.runners);
k.backoddprice = JSON.parse(k.backoddprice);
k.layoddprice = JSON.parse(k.layoddprice);

eventIndex = mappedEventIds.indexOf(k.eventid);
if(eventIndex === -1){
//if not already mapped
mappedEventIds.push(k.eventid);
for(var i =0; i < k.runners.length; i++){
k.runners[i].backoddprice = k.backoddprice;

k.runners[i].layoddprice = k.layoddprice;
}
result.push(k);
}else{
//if mapped already
// I think this is duplicated, if yes. comment out or delete this else loop
for(var i =0; i < k.runners.length; i++){
k.runners[i].backoddprice = k.backoddprice;
k.runners[i].layoddprice = k.layoddprice;
result[eventIndex].runners.push(k.runners[i]);
}

}

});
console.log("result", result);

关于javascript - 如何减少json数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54452880/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com