美文网首页
json数据按照同一年月时间归类

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

作者: 我在南极喝可乐 | 来源:发表于2022-08-08 15:34 被阅读0次

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

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

          })

数据格式变化如下图

相关文章

网友评论

      本文标题:json数据按照同一年月时间归类

      本文链接:https://www.haomeiwen.com/subject/hvvwwrtx.html