方式一
let newArr = [];
srcTrackVoList.forEach((item, i) => {
let index = -1;
let isExists = newArr.some((newItem, j) => {
if (item.date == newItem.date) {
index = j;
return true;
}
});
if (!isExists) {
newArr.push({
date: item.date,
subList: [item]
});
} else {
newArr[index].subList.push(item);
}
});
console.log(newArr);
方式二 // 格式化返回數(shù)據(jù)列表
formatTrackList(srcTrackVoList) {
let list = [];
srcTrackVoList.forEach(ele => {
ele.checked = false;
ele.date = dayjs(parseInt(ele.date)).format("YYYY.MM.DD");
ele.startDateTime = dayjs(parseInt(ele.startDateTime)).format("HH:mm");
ele.endDateTime = dayjs(parseInt(ele.endDateTime)).format("HH:mm");
ele.mile = ele.mile.toFixed(2);
ele.totalDate = ele.totalDate.replace("分", "min").replace("小時", "h");
const item = list.find(day => day.date === ele.date);
if (item) {
item.tracks.push(ele);
} else {
const day = {
date: ele.date,
tracks: [ele]
};
list.push(day);
}
});
return list;
},