import _from "lodash";
// 需求:把listData数组里相同月(①②)的数据统一整合在六月的数组里
const listData=[
{
inTime: "2023-06-05 10:30:00", ①
type: 1
},
{
inTime: "2023-06-04 10:35:00", ②
type: 5
},
{
inTime: "2023-05-31 13:30:00", ③
type: 5
}];
// 遍历:把item.inTime.substring(0,7)作为条件去listData数组中的每一个对象里查找
// 比如:2023-06是①②的inTime都符合条件,结果:[2023-06:[{①},{②}]],继续查找...
var listDataHour = _.groupBy(listData,(item)=>{
return (item.inTime.substring(0,7)) //return裁剪出来2023-06 2023-05
});
console.log结果如下:
[2023-06:[{
inTime: "2023-06-05 10:30:00",
type: 1
},
{
inTime: "2023-06-04 10:35:00",
type: 5
}],
2023-05:[{
inTime: "2023-05-31 13:30:00",
type: 5
}],
]
效果图: