如何减少json数据

时间:2019-01-31 03:29:31

标签: javascript json

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相同,那么应该推送运行器数组中的数据,我希望预期的输出应如下所示,我已经尝试过但是我无法达到预期的效果。我们将不胜感激。谢谢

  

我希望每个单独的对象都这样

{
    "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}]"
    }
  ]
}

这是我到目前为止尝试过的,数据非常大,我无法在此处创建代码段

3 个答案:

答案 0 :(得分:1)

这是一个利用Event类的版本,该类产生一个keyRunner()。 keyRunner是来自runners数组的运行程序,其ID与包含的对象中的ID相匹配。 (即使我不理解语义,为了匹配所需的输出,此方法也将backodpricelayodprice添加到结果对象中。)

(关于mergedRunners与Runners数组的删除点)

希望这是您正在寻找的东西,或者至少不是希望我能在解决此类问题时证明抽象和仔细的变量命名的价值。

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}]"
  }
];

class Event {
  constructor(eventData) {
    Object.assign(this, eventData);
    this.selectionid = parseInt(eventData.selectionid);
    this.runnerData = JSON.parse(eventData.runners);
    this.runners = [this.keyRunner()];
  }
  keyRunner() {
    let runner = this.runnerData.find(e => {
        return e.selectionId === this.selectionid;
    });
    runner.backoddprice = this.backoddprice;
    runner.layoddprice = this.layoddprice;
    return runner;
  }
  asPrintable() {
      let printable = Object.assign(this, {});
      delete printable.runnerData;
      return printable
  }
}

let mergedEvents = [];
c.forEach(eventData => {
    let event = new Event(eventData);
    let existing = mergedEvents.find(e => {
          return e.eventid === event.eventid;
    });
    if (existing) {
        existing.runners.push(event.keyRunner());
    } else {
        mergedEvents.push(event);
    }
});

console.log(mergedEvents.map(me => me.asPrintable()));

答案 1 :(得分:1)

我无法关联您的输入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);

答案 2 :(得分:0)

也许不是理想的解决方案,但是如果我对您的理解很好,请尝试以下操作:

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 saveEvents = [];
let newJson = [];

const myRunner = function ( selectionid, runners, data ) {
    let myRunner;
    for ( const runner in runners ) {
        const runnerData = runners[ runner ];
        if ( runnerData.selectionId == selectionid ) {
            myRunner = runnerData;
            
            break;
        }
    }
    
    return {
        'selectionid': myRunner.selectionId,
        'selectionname': myRunner.runnerName,
        'backoddprice': data.backoddprice,
        'layoddprice': data.layoddprice
    };
};

let myRunnerIndex = -1;
let myNewRunner = 0;

for ( const index in c ) {
    const data = c[ index ];
    for ( const key in data ) {
        
        if ( key === 'eventid' ) {
            const eventid = data.eventid;
            const selectionid = data.selectionid;
            const runners = JSON.parse( data.runners );
            
            // Check if eventid is already included
            if ( !saveEvents.includes( eventid ) ) {
                myNewRunner = 1;
                
                saveEvents.push( eventid );
                newJson.push( {
                    'eventid': eventid,
                    'eventname': data.eventname,
                    'runners': {
                        '0': myRunner( selectionid, runners, data )
                    }
                } );
                
                ++myRunnerIndex;
            } else {
                // If included then just update runners
                newJson[ myRunnerIndex ].runners[ myNewRunner ] = myRunner( selectionid, runners, data );
                
                ++myNewRunner;
            }
        }
    }
}

console.log(JSON.stringify(newJson, null, 4))