zoukankan      html  css  js  c++  java
  • xgqfrms™, xgqfrms® : xgqfrms's offical website of GitHub!

    how to group date array by month in javascript

    https://stackoverflow.com/questions/14446511/most-efficient-method-to-groupby-on-an-array-of-objects

    
    function groupBy(list, keyGetter) {
        const map = new Map();
        list.forEach((item) => {
             const key = keyGetter(item);
             const collection = map.get(key);
             if (!collection) {
                 map.set(key, [item]);
             } else {
                 collection.push(item);
             }
        });
        return map;
    }
    
    // example usage
    
    const pets = [
        {type:"Dog", name:"Spot"},
        {type:"Cat", name:"Tiger"},
        {type:"Dog", name:"Rover"}, 
        {type:"Cat", name:"Leo"}
    ];
        
    const grouped = groupBy(pets, pet => pet.type);
        
    console.log(grouped.get("Dog")); 
    //  [{type:"Dog", name:"Spot"}, {type:"Dog", name:"Rover"}]
    
    console.log(grouped.get("Cat"));
    //  [{type:"Cat", name:"Tiger"}, {type:"Cat", name:"Leo"}]
        
    
    

    https://stackoverflow.com/questions/41888837/js-group-by-month-of-date-values-objects-in-an-array

    
    const dates = [
        {date: "2017-01-01", num: "2"},
        {date: "2017-01-02", num: "3"},
        {date: "2017-02-04", num: "6"},
        {date: "2017-02-05", num: "15"}
    ],
    
    let groupKey = 0;
    
    groups = dates.reduce(function (r, o) {
            var m = o.date.split(('-'))[1];
            (r[m])? r[m].data.push(o) : r[m] = {group: String(groupKey++), data: [o]};
            return r;
        }, {});
    
    const result = Object.keys(groups).map(k => groups[k]);
    
    console.log(result);
    
    

    OK

    
    const json = [
      {
        "id": 191701,
        "productId": 13602,
        "activityEventId": 1623852,
        "ticketCategoryId": 5246618,
        "ticketGroupId": 11798619,
        "start": 1585670400000,
        "end": 1585670400000,
        "ticketsNumber": 100,
        "lowPrice": 127.00,
        "status": 0,
        "priceLowest": false,
        "hasTicket": true,
        "availableNumbers": [
          1,
          2,
          3,
          4,
          5,
          6
        ],
        order: "C"
      },
      {
        "id": 191837,
        "productId": 13602,
        "activityEventId": 1623896,
        "ticketCategoryId": 5246754,
        "ticketGroupId": 11798755,
        "start": 1585497600000,
        "end": 1585497600000,
        "ticketsNumber": 100,
        "lowPrice": 127.00,
        "status": 0,
        "priceLowest": false,
        "hasTicket": true,
        "availableNumbers": [
          1,
          2,
          3,
          4,
          5,
          6
        ],
        order: "A"
      },
      {
        "id": 191812,
        "productId": 13602,
        "activityEventId": 1623891,
        "ticketCategoryId": 5246729,
        "ticketGroupId": 11798730,
        "start": 1585584000000,
        "end": 1585584000000,
        "ticketsNumber": 100,
        "lowPrice": 127.00,
        "status": 0,
        "priceLowest": false,
        "hasTicket": true,
        "availableNumbers": [
          1,
          2,
          3,
          4,
          5,
          6
        ],
        order: "B"
      },
    ];
    
    const sortArray = json.sort((a, b) => a.start > b.start ? 1 : -1);
    // asc order 升序
    
    const calendarArray = sortArray.map(obj => {
      const date = new Date(obj.start);
      const year  = date.getFullYear();
      const month  = date.getMonth() + 1;
      return {
        year,
        month,
        ...obj,
      };
    });
    
    
    // group date by month / year
    
    
    const groupArrayByKey = (items, key) => items.reduce(
      (result, item) => ({
        ...result,
        [item[key]]: [
          ...(result[item[key]] || []),
          item,
        ],
      }),
      {},
    );
    
    
    const groupArray = groupArrayByKey(calendarArray, `month`);
    
    

    Flag Counter

    ©xgqfrms 2012-2020

    www.cnblogs.com 发布文章使用:只允许注册用户才可以访问!


  • 相关阅读:
    React性能优化 渲染20000多条checkbox组件,点击选择checkbox组件的时候特别卡,解决办法
    react网页多语言(react-intl-universal)
    个人作品
    程序员和产品经理的那些事
    three.js 根据不同的图片做不同的动画特效 科技感十足
    互联网行业最佳产品开发流程 推荐!
    类似于YouTube、Medium、知乎等网站使用的进度条插件 NProgress
    js模拟点击下载文件到本地
    MySQL记录操作(增删改)
    MySQL表的操作
  • 原文地址:https://www.cnblogs.com/xgqfrms/p/12600875.html
Copyright © 2011-2022 走看看