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`);


xgqfrms 2012-2020

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


最新文章

  1. Best Time to Buy and Sell Stock III [LeetCode]
  2. 介绍一些chrome 好用的插件和快捷键
  3. KVM 介绍(8):使用 libvirt 迁移 QEMU/KVM 虚机和 Nova 虚机 [Nova Libvirt QEMU/KVM Live Migration]
  4. sublime配置markdown
  5. SU Demos-06Selecting Traces
  6. SSH框架总结(框架分析+环境搭建+实例源码下载) 《转》
  7. 客户端禁用cookies后session是否还起效果
  8. Entity Framework调用表值函数实现全文检索?
  9. 255. Verify Preorder Sequence in Binary Search Tree
  10. eclipse mingw cpp开发环境
  11. solaris X86-64下一个ORACLE战斗11.2.0.3.8在一波折叠补丁
  12. Eclipse 枚举类报错
  13. 【Spring源码解析】—— 依赖注入结合SpringMVC Demo-xml配置理解
  14. 51Nod 1058 N的阶乘的长度
  15. VS2017上执行VS2013项目错误MSB802之解决方案
  16. Python爬虫利器二之Beautiful Soup的用法
  17. 学习笔记36—坚果云 | Papership或Zotero使用webDAV验证服务器不成功怎么办?
  18. ODOO v10.0 自动生成财务凭证的科目设置
  19. centos7 脚本搭建SVN
  20. 【Canal源码分析】整体架构

热门文章

  1. 使用eventfd创建一个用于事件通知的文件描述符
  2. Linux下编译安装源码包软件 configure ,make, make install, make test/check, make clean 假目标
  3. loj10008家庭作业
  4. C#9.0:Records
  5. SANGFOR AC配置AD域单点登录(二)----AD域侧配置及单点登录认证、注销测试
  6. idea2018 快捷键
  7. Scala面向对象—类详解2(继承相关)
  8. Grakn Forces 2020
  9. hdu3559 Frost Chain (概率dp+记忆化搜索)
  10. Codeforces Global Round 8 A. C+=(贪心)