为了账号安全,请及时绑定邮箱和手机立即绑定

选日期的时候提取相邻的日期天数?

/ 猿问

选日期的时候提取相邻的日期天数?

qq_浅仓小星_0 2019-12-31 17:48:40

checkList:[
                    {
                        date:'2019-01-01',
                        num:1,
                        roomId:1
                    },
                    {
                        date:'2019-01-02',
                        num:1,
                        roomId:1
                    },
                    {
                        date:'2019-01-03',
                        num:1,
                        roomId:1
                    },
                    {
                        date:'2019-01-05',
                        num:1,
                        roomId:1
                    },
                    {
                        date:'2019-01-03',
                        num:1,
                        roomId:2
                    },
                    {
                        date:'2019-01-04',
                        num:1,
                        roomId:2
                    }
                ],
                checkList2:[
                    {
                        date:'2019-01-01',
                        num:3,
                        roomId:1
                    },
                    {
                        date:'2019-01-05',
                        num:1,
                        roomId:1
                    },
                    {
                        date:'2019-01-03',
                        num:2,
                        roomId:2
                    }
                ]
最终我想把list这个初始数据变成list2这样的数据要怎么弄,num是天数,roomId是房间idhttp://img2.sycdn.imooc.com/5e0b195300012be904570693.jpg

查看完整描述

2 回答

?
橋本奈奈未
Object.values(checkList.reduce((res, v) => {    
    const key = v.date;    
    if(res[key]) res[key].num++;    
    else res[key] = v;    
    return res;
},{}))


查看完整回答
反对 2019-12-31
  • qq_浅仓小星_0
    qq_浅仓小星_0
    就是要把checkList变成checkList2这样的数据,连着选的日期数据合成一条,然后天数累加
  • 橋本奈奈未
    橋本奈奈未
    不太理解这个输出。是要合并date一样的值吗?roomId不一样呢?看输出好像roomId不一样只是把前面的id给替换了。总之要合并的话可以使用reduce来操作,以唯一的键如date作为key,然后再将对象的值输出为数组

添加回答

回复

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信