- 思路:
- 由于时间都是按照顺序排列的,所以,就顺着这条时间线来判断每一个item的时间是否在这个每一组里;
- 翠花,上图~!
![请添加图片描述](https://i-blog.csdnimg.cn/blog_migrate/af65e1df22e9806ea8b840ddf2a7fb9d.png)
const _processedHistory = [];
let _createdAtFlag;
let _currentProcessedItem = {};
_data.forEach((historyItem, index)=>{
const _currentHistoryCreatedAt = new Date(historyItem.created_at);
if (!_createdAtFlag) {
_createdAtFlag = _currentHistoryCreatedAt;
_currentProcessedItem.title = _currentHistoryCreatedAt.setHours(0, 0, 0, 0);
_currentProcessedItem.items = [ historyItem ];
if (_data.length === index + 1) {
_processedHistory.push(_currentProcessedItem);
}
} else if (_createdAtFlag.setHours(0, 0, 0, 0) < _currentHistoryCreatedAt &&
_currentHistoryCreatedAt < _createdAtFlag.setHours(23, 59, 59, 999)) {
_currentProcessedItem.items.push(historyItem);
if (_data.length === index + 1) {
_processedHistory.push(_currentProcessedItem);
}
} else {
_processedHistory.push(_currentProcessedItem);
_currentProcessedItem = {};
_createdAtFlag = _currentHistoryCreatedAt;
_currentProcessedItem.title = _currentHistoryCreatedAt.setHours(0, 0, 0, 0);
_currentProcessedItem.items = [ historyItem ];
}
});
setHistory(_processedHistory);