js 数据按照日期内的小时分段进行重新分组

2020-11-23  本文已影响0人  缓慢的蜗牛
let resData=[
{
daofang_text: "来访单位王旭",
file_url: "https://static.zyzhedu.com/files/M00/03/14/CgrA21-7goCAA6p0AAAHDt11D2M109.png",
has_leave: 2,
leave_time: null,
ru_time: "2020-11-23 17:36:18",
school_id: "c6f7e41a-4d37-11ea-9117-00163e14ebe3",
visitor_name: "王旭",
visitor_purpose: "来访事由 王旭",
visitor_tel: "1891****044"
},
{
daofang_text: "来访单位王旭",
file_url: "https://static.zyzhedu.com/files/M00/03/14/CgrA21-7goCAA6p0AAAHDt11D2M109.png",
has_leave: 2,
leave_time: null,
ru_time: "2020-11-23 15:36:18",
school_id: "c6f7e41a-4d37-11ea-9117-00163e14ebe3",
visitor_name: "王旭",
visitor_purpose: "来访事由 王旭",
visitor_tel: "1891****044"
},
{
daofang_text: "来访单位王旭",
file_url: "https://static.zyzhedu.com/files/M00/03/14/CgrA21-7goCAA6p0AAAHDt11D2M109.png",
has_leave: 2,
leave_time: null,
ru_time: "2020-11-23 15:36:18",
school_id: "c6f7e41a-4d37-11ea-9117-00163e14ebe3",
visitor_name: "王旭",
visitor_purpose: "来访事由 王旭",
visitor_tel: "1891****044"
},
{
daofang_text: "来访单位王旭",
file_url: "https://static.zyzhedu.com/files/M00/03/14/CgrA21-7goCAA6p0AAAHDt11D2M109.png",
has_leave: 2,
leave_time: null,
ru_time: "2020-11-23 14:36:18",
school_id: "c6f7e41a-4d37-11ea-9117-00163e14ebe3",
visitor_name: "王旭",
visitor_purpose: "来访事由 王旭",
visitor_tel: "1891****044"
}
]
// 时间段数组
  let timeSlotArr = [];
  for (let i = 0; i < resData.length; i++) {
       timeSlotArr.push(
        resData[i].ru_time.split(" ")[1].split(":")[0],
        )
    }
// 数组去重 //从字符串转为数组
   var timeSlotArrNew = Array.from(new Set(timeSlotArr));
   // 返回小时时间段重组成新的数组
   let resDataTimeSlot = [];
   for (let x = 0; x < timeSlotArrNew.length; x++) {
         resDataTimeSlot.push({
              timeSlotItem: timeSlotArrNew[x],
               visitorRecordItem: [],
         });
   }
// 循环时间段 时间段内对应的数据push 到重新组织的数组中
  for (let m = 0; m < resDataTimeSlot.length; m++) {
        for (let y = 0; y < resData.length; y++) {
             if (resDataTimeSlot[m].timeSlotItem === resData[y].ru_time.split(" ")[1].split(":")[0]) {
                resDataTimeSlot[m].visitorRecordItem.push(resData[y]);
             }
        }
   }
   console.log("最后的数组resDataTimeSlot", resDataTimeSlot);

如有更好的方法,请您评论

上一篇下一篇

猜你喜欢

热点阅读