后端返回一维数组,前端合并单元格
loadList (data, sameKeys, startKey = 0) {
if (startKey === sameKeys.length) {
return data
}
const list = []
const sameDateKey = 'same' + sameKeys[startKey] + 'Len'
const sameFilter = sameKeys.filter((item, index)=>{
return index <= startKey
})
data.forEach(item => {
const findIndex = list.findIndex((fid) => {
return fid[sameDateKey] && sameFilter.every(eve=>{
return item[eve] === fid[eve]
})
});
if (findIndex < 0) {
const targItem = Object.assign({}, item, {
[sameDateKey]: 1
})
list.push(targItem)
}
else {
const findItem = list[findIndex]
list.push(Object.assign({}, item, {
[sameDateKey]: 0
}))
list[findIndex][sameDateKey] = findItem[sameDateKey] + 1
}
})
startKey++
return this.loadList(list, sameKeys, startKey)
}
使用方法,一层一层判断
this.tableData = this.loadList(res.data.list || [], ['teacherName', 'courseName', 'gradeName'])