首页 新闻 会员 周边 捐助

typescript 循环列表获取乘积的和

0
[已解决问题] 解决于 2019-11-19 14:13

一个Array<Model>,比如

[{
    " Items": [{
        "RoomNum": 2
    }, {
        "RoomNum": 6
    }],

    "Dates": {
        "From": "2018-05-21T16:00:00.000Z",
        "To": "2018-05-26T16:00:00.000Z"
    }
}, {
    " Items": [{
        "RoomNum": 3
    }],

    "Dates": {
        "From": "2018-05-25T16:00:00.000Z",
        "To": "2018-05-27T16:00:00.000Z"
    }
}]

differenceInDays用的date-fns.js 来获取from和to的差值(天数)

现在我想获取

(items.RoomNum  * differenceInDays(item.TravelDates.To,item.TravelDates.From))
+(items.RoomNum  * differenceInDays(item.TravelDates.To,item.TravelDates.From))...

举个例子,我想获取

2*5 + 6*5 +3*2  

我应该怎么写

YamatAmain的主页 YamatAmain | 初学一级 | 园豆:23
提问于:2019-11-19 08:51
< >
分享
最佳答案
0
private buildNightNum(
    groups: Dictionary<Model[]>,
    item: string
  ) {
    let differenceFromTo = groups[item]
      .filter(t => t.Dates)
      .reduce(
        (a, b) => a + differenceInDays(b.Dates.To, b.Dates.From),
        0
      );
    let roomNum = groups[item]
      .filter(t => t.Items.filter(s => s.RoomNum))
      .reduce(
        (a, b) =>
          a +
          b.Items.filter(s => s.RoomNum)
            .map(s => s.RoomNum)
            .reduce((c, d) => c + d, 0),
        0
      ); 
    return   differenceFromTo* roomNum ;
  }
YamatAmain | 初学一级 |园豆:23 | 2019-11-19 14:13
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册