Lodash集团 [英] Lodash groupBy with moment

查看:105
本文介绍了Lodash集团的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在今天,本周,本月以及之前的几个月之前对一堆日期进行分组.但是,在我到达这一点之前,我都在努力按月进行基本分组.

I am trying to group a bunch of dates by today, this week, this month and then previous months. However before I even get to this point I am struggling to get even a basic grouping by month.

const data = [{
 campaign: "Charles",
 company_ID: "1",
 coreURL: "http://www.test77.com",
 createdAt: "2017-11-06T20:45:56.931Z",
 owner: "K7xTxu7PRDCuhFZRC",
 updatedAt: "2017-09-06T20:45:56.931Z",
 _id: "6gsb42PSSJt7PgsDG"
}, {
 campaign: "Charles",
 company_ID: "1",
 coreURL: "http://www.test66,com",
 createdAt: "2017-11-06T20:46:27.744Z",
 owner: "K7xTxu7PRDCuhFZRC",
 updatedAt: "2017-10-06T20:46:27.744Z",
 _id: "Md4wCnEsrQrWApnLS"
}, {
 campaign: "Gary",
 company_ID: "1",
 coreURL: "http://www.test55,com",
 createdAt: "2017-11-06T20:46:27.744Z",
 owner: "K7xTxu7PRDCuhFZRC",
 updatedAt: "2017-07-06T20:46:27.744Z",
 _id: "5p44uiwRgqp35YXRf"
}
];

const grouper = 'updatedAt';

let groupedData = _
  .chain(data)
  .groupBy(datum => moment(datum.grouper).format("MM")) 

但是,这似乎仅按"11"(今天月份)进行分组

However this only seems to group by "11" (todays Month)

任何帮助将不胜感激

推荐答案

datum.grouperundefined,因此moment(datum.grouper)返回当前日期.

datum.grouper is undefined so moment(datum.grouper) is returning the current date.

您应该改用datum[grouper]或类似的_.property('updatedAt').

关于您如何将日期分为今天,本周,本月和其他月份的问题,请尝试以下操作:

Regarding your question about how to separate the dates into today, this week, this month, and other months, please try the below:

const data = [{
  campaign: "Charles",
  company_ID: "1",
  coreURL: "http://www.test77.com",
  createdAt: "2017-11-06T20:45:56.931Z",
  owner: "K7xTxu7PRDCuhFZRC",
  updatedAt: "2017-09-06T20:45:56.931Z",
  _id: "6gsb42PSSJt7PgsDG"
}, {
  campaign: "Charles",
  company_ID: "1",
  coreURL: "http://www.test66,com",
  createdAt: "2017-11-06T20:46:27.744Z",
  owner: "K7xTxu7PRDCuhFZRC",
  updatedAt: "2017-10-06T20:46:27.744Z",
  _id: "Md4wCnEsrQrWApnLS"
}, {
  campaign: "Gary",
  company_ID: "1",
  coreURL: "http://www.test55,com",
  createdAt: "2017-11-06T20:46:27.744Z",
  owner: "K7xTxu7PRDCuhFZRC",
  updatedAt: "2017-07-06T20:46:27.744Z",
  _id: "5p44uiwRgqp35YXRf"
}, {
  campaign: "Fred",
  company_ID: "1",
  coreURL: "http://www.test55,com",
  createdAt: "2017-11-06T20:46:27.744Z",
  owner: "K7xTxu7PRDCuhFZRC",
  updatedAt: "2017-11-15T03:46:27.744Z",
  _id: "5p44uiwRgqp35YXRf"
}, {
  campaign: "Fred",
  company_ID: "1",
  coreURL: "http://www.test55,com",
  createdAt: "2017-11-06T20:46:27.744Z",
  owner: "K7xTxu7PRDCuhFZRC",
  updatedAt: "2017-11-03T20:46:27.744Z",
  _id: "5p44uiwRgqp35YXRf"
}, {
  campaign: "Fred",
  company_ID: "1",
  coreURL: "http://www.test55,com",
  createdAt: "2017-11-06T20:46:27.744Z",
  owner: "K7xTxu7PRDCuhFZRC",
  updatedAt: "2017-11-13T20:46:27.744Z",
  _id: "5p44uiwRgqp35YXRf"
}, {
  campaign: "Fred",
  company_ID: "1",
  coreURL: "http://www.test55,com",
  createdAt: "2017-11-06T20:46:27.744Z",
  owner: "K7xTxu7PRDCuhFZRC",
  updatedAt: "2017-11-09T20:46:27.744Z",
  _id: "5p44uiwRgqp35YXRf"
}];

const groupProp = _.property('updatedAt');

let determineGroup = value => {
  // remove '2017-11-15' to actually use current date 
  const now = moment('2017-11-15T10:00:03Z').startOf('day');

  if (value.isSame(now, 'day')) {
    return 'today';
  }
  if (value.isAfter(now.clone().subtract(7, 'days').startOf('day'))) {
    return 'this week';
  }
  if (value.isSame(now, 'month')) {
    return 'this month';
  }
  return value.format('MM');
};

let groupedData = _
  .chain(data)
  .groupBy(datum => determineGroup(moment(groupProp(datum))))
  .value()

console.log(groupedData);

<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.19.2/moment.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.4/lodash.min.js"></script>

这篇关于Lodash集团的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆