// 把日期按照月份放在一组
handleData (arr) {
let newArr = []
let n = 0
let yearMonths = ''
let first = arr[0].operation_time.split('-')
yearMonths = first[1] + '月'
newArr[n] = [arr[0]]
newArr[n].yearMonths = yearMonths // 先将第一组数据放进数组,然后判断后一组数据与前一组数据,年月是否相同,相同就放进该组数组,不相同则新建一个数组
for (let i = 1; i < arr.length; i++) {
let current = arr[i].operation_time.split('-')
let before = arr[i - 1].operation_time.split('-')
yearMonths = current[1] + '月'
if (current[0] === before[0] && current[1] === before[1]) {
newArr[n].push(arr[i])
} else {
n++
newArr[n] = []
newArr[n].yearMonths = yearMonths
newArr[n].push(arr[i])
}
}
return newArr
}
该方法只对有序数组有效,使用前需要对数组进行排序
this.list.sort(function (a, b) {
let t1 = new Date(Date.parse(a.operation_time.replace(/-/g, '/')))
let t2 = new Date(Date.parse(b.operation_time.replace(/-/g, '/')))
return t2.getTime() - t1.getTime()
})
数据格式变化如下图
网友评论