Lodash GROUPBY与时刻(Lodash groupBy with moment)

2019-09-29 23:48发布

我想组一帮本月今天的日期,这一周,然后前几个月。 不过之前我甚至得到这一点我很努力通过每月能拿到连最基本的分组。

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”(当今月)似乎组

任何帮助将非常感激

Answer 1:

datum.grouperundefined这样moment(datum.grouper)将返回当前日期。

您应该改为使用datum[grouper]或像这样_.property('updatedAt')

关于你提到的有关如何将日期分成今天,本周,本月,以及其他月份疑问,请尝试以下:

 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> 



Answer 2:

也许你可以使用过滤器,如果你只需要当前的月,日,年。

const today = _.filter(data, d => moment(d.updatedAt).isSame(moment(), 'day'));
console.log(today);

const thisWeek = _.filter(data, d => moment(d.updatedAt).isSame(moment(), 'week'));
console.log(thisWeek);

const thisMonth = _.filter(data, d => moment(d.updatedAt).isSame(moment(), 'month'));
console.log(thisMonth);

或者GROUPBY如果你需要所有24个月(所有年份)

const byMonth = _.groupBy(data, d => moment(d.updatedAt).month());
console.log(byMonth);

https://jsbin.com/xidido/edit?js,console



Answer 3:

let groupedData = _
.chain(data)
.groupBy(record => moment(record.updatedAt).format("MM"))
.value();


文章来源: Lodash groupBy with moment