How do you group a set of data according to a particular field?

for example, I have a set of data

[
  {
    "time": "2018-03-01",
    "data": 1,
  },
  {
    "time": "2018-02-02",
    "data": 2,
  },
  {
    "time": "2018-01-01",
    "data": 3,
  },
  {
    "time": "2017-12-01",
    "data": 4,
  }, 
  {
    "time": "2017-11-02",
    "data": 5,
  }, 
  {
    "time": "2016-10-01",
    "data": 6,
  },
  {
    "time": "2016-9-01",
    "data": 7,
  }    
]

the actual business scenario is to group this set of data into groups of 5 items, but these 5 items are also divided into groups according to their dates and years

for example, the result of the above seven items is:

[
  {
    section: 1,
    sectionDatas: [
      {
        date: "2018",
        datas: [
          {
            "time": "2018-03-01",
            "data": 1,
          },
          {
            "time": "2018-02-02",
            "data": 2,
          },
          {
            "time": "2018-01-01",
            "data": 3,
          }
        ]
      },
      {
        date: "2017",
        datas: [
          {
            "time": "2017-12-01",
            "data": 4,
          }, 
          {
            "time": "2017-11-02",
            "data": 5,
          }
        ]
      }
    ]
  },
  {
    section: 2,
    sectionDatas: [
      {
        date: "2016",
        datas: [
          {
            "time": "2016-10-01",
            "data": 6,
          },
          {
            "time": "2016-9-01",
            "data": 7,
          } 
        ]
      }
    ]
  } 
]

how to write this algorithm. Ask for advice


< H1 > step < / H1 >

* step 1: sort, time from near to far
* step 2: split the array into five groups and return to the new array
* step 3: extract from the array and extract it at the same time to form the required data format

< H1 > Code < / H1 >
step 1: sort

var arr = new Object
data.forEach(function(d){
    var year = new Date(d.time).getFullYear()
    if(arr[year]){
        arr[year].push(d)
    }else{
        arr[year] = [d]
    }
})

provides an idea. If you modify it, you should be able to use

.
MySQL Query : SELECT * FROM `codeshelper`.`v9_news` WHERE status=99 AND catid='6' ORDER BY rand() LIMIT 5
MySQL Error : Disk full (/tmp/#sql-temptable-64f5-1b357a5-2bf92.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
MySQL Errno : 1021
Message : Disk full (/tmp/#sql-temptable-64f5-1b357a5-2bf92.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
Need Help?