json数据按照同一年月时间归类

 // 把日期按照月份放在一组

    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()

          })

数据格式变化如下图

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。