思路就是把history数组处理成一个相同日期是在同一个数组成员内,再调整一下 addHisList 函数就可以
demo 链接:https://codepen.io/liangxh052...
function handleHistory(history) {
const arr = history.map(item => {
return {
date: dateFormat(item.date, 'YYYY-MM-DD'),
list: [
{
SName: item.SName,
content: item.content
}
]
}
});
let result = [];
arr.forEach(item => {
if (result.some(every => every.date === item.date)) {
result.forEach(_ => {
if(_.date === item.date) {
_.list = _.list.concat(item.list)
}
})
} else {
result.push(item)
}
})
return result;
}
const history = [
{"type":"text","date":1592889320877,"SName":"xxx","content":"1"},
{"type":"text","date":1592889320877,"SName":"xxx","content":"2"},
{"type":"text","date":1592889320877,"SName":"xxx","content":"3"},
{"type":"text","date":1592889320877,"SName":"xxx","content":"4"},
{"type":"text","date":1592981108862,"SName":"xxx","content":"4"}
]
console.log(handleHistory(history))
function dateFormat (date, format, en) {
let self = new Date(date);
let fmt = format || 'YYYY-MM-DD hh:mm:ss';
let o = {
'M+': self.getMonth() + 1,
'D+': self.getDate(),
'h+': self.getHours(),
'H+': self.getHours() % 12 === 0 ? 12 : self.getHours() % 12,
'N': self.getHours() >= 12 ? (en ? 'PM' : '\u4e0b') : (en ? 'AM' : '\u4e0a'),
'm+': self.getMinutes(),
's+': self.getSeconds(),
'q+': Math.floor((self.getMonth() + 3) / 3),
'S': self.getMilliseconds()
};
if (/(Y+)/.test(fmt)) {
fmt = fmt.replace(RegExp.$1, (self.getFullYear() + '').substr(4 - RegExp.$1.length));
}
for (let k in o) {
if (new RegExp('(' + k + ')').test(fmt)) {
fmt = fmt.replace(RegExp.$1, (RegExp.$1.length === 1) ? (o[k]) : (('00' + o[k]).substr(('' + o[k]).length)));
}
}
let week = ['\u65e5', '\u4e00', '\u4e8c', '\u4e09', '\u56db', '\u4e94', '\u516d'];
let weekEn = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'];
if (/(d+)/.test(fmt)) {
if (en) {
fmt = fmt.replace(RegExp.$1, (RegExp.$1.length > 1) ? weekEn[self.getDay()].substr(0, 3) : weekEn[self.getDay()]);
} else {
fmt = fmt.replace(RegExp.$1, ((RegExp.$1.length > 1) ? (RegExp.$1.length > 2 ? '\u661f\u671f' : '\u5468') : '') + week[self.getDay()]);
}
}
return fmt;
};