怎么对一组数据按照某个特定字段进行分组?

比如我有一组数据


[

  {

    '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,

  }    

]

实际业务场景是对这组数据进行分组,每5条一组 但是这5条还要根据他们的日期年份分小组


比如上面这7条分出来结果就是:


[

  {

    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,

          } 

        ]

      }

    ]

  } 

]

这个算法该怎么写呢。。求指教


慕森卡
浏览 1195回答 2
2回答

喵喔喔

步骤*第一步:排序,时间从近到远*第二步:分割,将数组分成5个一组,返回新数组*第三步:抽离,将相同时间抽离出来组成要求上的数据格式代码第一步:排序&nbsp; &nbsp; var compare = function (prop) {&nbsp; &nbsp; &nbsp; &nbsp; return function (obj1, obj2) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var val1 = obj1[prop];&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var val2 = obj2[prop];&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (!isNaN(Number(val1)) && !isNaN(Number(val2))) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; val1 = Number(val1);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; val2 = Number(val2);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (val1 < val2) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return 1;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } else if (val1 > val2) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return -1;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return 0;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; //用法&nbsp; &nbsp; arr.sort(compare("time"))第二步:分割//分割&nbsp; &nbsp; Array.prototype.chunk = function(size) {&nbsp; &nbsp; &nbsp; &nbsp; var array = this;&nbsp; &nbsp; &nbsp; &nbsp; const length = array.length&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; if (!length || !size || size < 1) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return []&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; let index = 0&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; let resIndex = 0&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; let result = new Array(Math.ceil(length / size))&nbsp; &nbsp; &nbsp; &nbsp; while (index < length) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; result[resIndex++] = array.slice(index, (index += size))&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; return result&nbsp; &nbsp; }&nbsp; &nbsp; //用法&nbsp; &nbsp; arr.chunk(5)第三步:抽离&nbsp; &nbsp; //抽离&nbsp; &nbsp; function sortArr(arr, str) {&nbsp; &nbsp; &nbsp; &nbsp; var _arr = [],&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; _t = [],&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // 临时的变量&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; _tmp;&nbsp; &nbsp; &nbsp; &nbsp; // 按照特定的参数将数组排序将具有相同值得排在一起&nbsp; &nbsp; &nbsp; &nbsp; arr = arr.sort(function(a, b) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var s = a[str],&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; t = b[str];&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return s < t ? -1 : 1;&nbsp; &nbsp; &nbsp; &nbsp; });&nbsp; &nbsp; &nbsp; &nbsp; if ( arr.length ){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // _tmp = arr[0][str];&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; _tmp = new Date(arr[0][str]).getFullYear()&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; // 将相同类别的对象添加到统一个数组&nbsp; &nbsp; &nbsp; &nbsp; for (let i=0;i<arr.length;i++) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if(new Date(arr[i][str]).getFullYear()===_tmp){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // if ( arr[i][str] === _tmp ){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; _t.push( arr[i] );&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; _tmp = new Date(arr[i][str]).getFullYear();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; _arr.push( _t );&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; _t = [arr[i]];&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; // 将最后的内容推出新数组&nbsp; &nbsp; &nbsp; &nbsp; _arr.push(_t);&nbsp; &nbsp; &nbsp; &nbsp; return _arr;&nbsp; &nbsp; }&nbsp; &nbsp; //用法,这里仅对一维数组使用&nbsp; &nbsp; sortArr( arr, 'time')&nbsp; &nbsp; //我将数据一二步处理后开始二叉数循环&nbsp; &nbsp; var cc = arr.sort(compare("time")).chunk(5)&nbsp; &nbsp; var _cc = []&nbsp; &nbsp; for(let i=0;i<cc.length;i++){&nbsp; &nbsp; &nbsp; &nbsp; //抽离&nbsp; &nbsp; &nbsp; &nbsp; var _datas=sortArr( cc[i], 'time')&nbsp; &nbsp; &nbsp; &nbsp; //根据所给数据结构进行赋值&nbsp; &nbsp; &nbsp; &nbsp; _cc.push({section:i+1, sectionDatas:[]})&nbsp; &nbsp; &nbsp; &nbsp; for(let o=0;o<_datas.length;o++){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; _cc[i].sectionDatas.push({&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; data:new Date(_datas[o][0].time).getFullYear(),&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; datas:[]&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; })&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; for(let p=0;p<_cc[i].sectionDatas.length;p++){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; _cc[i].sectionDatas[p].datas=sortArr(cc[i], 'time')[p]&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }最近刚研究的函数式编程功力不够,谨慎使用&nbsp; &nbsp; class Functor {&nbsp; &nbsp; &nbsp; &nbsp; constructor(data){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; this.data = data&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; map(data){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return new Functor(data)&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; //排序&nbsp; &nbsp; &nbsp; &nbsp; compare(prop) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var result = this.data.sort(function (obj1, obj2) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var val1 = obj1[prop];&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var val2 = obj2[prop];&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (!isNaN(Number(val1)) && !isNaN(Number(val2))) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; val1 = Number(val1);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; val2 = Number(val2);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (val1 < val2) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return 1;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } else if (val1 > val2) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return -1;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return 0;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; })&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return this.map(result)&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; //分割&nbsp; &nbsp; &nbsp; &nbsp; chunk(size) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var array = this.data;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //获取数组的长度,如果你传入的不是数组,那么获取到的就是undefined&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; const length = array.length&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //判断不是数组,或者size没有设置,size小于1,就返回空数组&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (!length || !size || size < 1) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return []&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //核心部分&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; let index = 0 //用来表示切割元素的范围start&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; let resIndex = 0 //用来递增表示输出数组的下标&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //根据length和size算出输出数组的长度,并且创建它。&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; let result = new Array(Math.ceil(length / size))&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //进行循环&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; while (index < length) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //循环过程中设置result[0]和result[1]的值。该值根据array.slice切割得到。&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; result[resIndex++] = array.slice(index, (index += size))&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //输出新数组&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // return result&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return this.map(result)&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; //抽离&nbsp; &nbsp; &nbsp; &nbsp; sortArr(arr, str) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var _arr = [],&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; _t = [],&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // 临时的变量&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; _tmp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // 按照特定的参数将数组排序将具有相同值得排在一起&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; arr = arr.sort(function(a, b) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var s = a[str],&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; t = b[str];&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return s < t ? -1 : 1;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; });&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if ( arr.length ){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // _tmp = arr[0][str];&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; _tmp = new Date(arr[0][str]).getFullYear()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // 将相同类别的对象添加到统一个数组&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for (let i=0;i<arr.length;i++) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if(new Date(arr[i][str]).getFullYear()===_tmp){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // if ( arr[i][str] === _tmp ){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; _t.push( arr[i] );&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; _tmp = new Date(arr[i][str]).getFullYear();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; _arr.push( _t );&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; _t = [arr[i]];&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // 将最后的内容推出新数组&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; _arr.push(_t);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return _arr;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; //转指定json格式&nbsp; &nbsp; &nbsp; &nbsp; dataToJSON(){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var _json = []&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var _this = this;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; this.data.forEach(function(item,i){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var _datas=_this.sortArr( item, 'time')&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; _json.push({section:i+1, sectionDatas:[]})&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for(let o=0;o<_datas.length;o++){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; _json[i].sectionDatas.push({&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; data:new Date(_datas[o][0].time).getFullYear(),&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; datas:[]&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; })&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for(let p=0;p<_json[i].sectionDatas.length;p++){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; _json[i].sectionDatas[p].datas=_datas[p]&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; })&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return _json&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; Functor.of = function(data){&nbsp; &nbsp; &nbsp; &nbsp; return new Functor(data)&nbsp; &nbsp; }使用方法Functor.of(a).compare("time").chunk(5).dataToJSON() //返回一个新的值,不改变原来数据

德玛西亚99

var arr = new Objectdata.forEach(function(d){&nbsp; &nbsp; var year = new Date(d.time).getFullYear()&nbsp; &nbsp; if(arr[year]){&nbsp; &nbsp; &nbsp; &nbsp; arr[year].push(d)&nbsp; &nbsp; }else{&nbsp; &nbsp; &nbsp; &nbsp; arr[year] = [d]&nbsp; &nbsp; }})提供个思路,修改一下应该能用
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript