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",
pany_ID: "1",
coreURL: "",
createdAt: "2017-11-06T20:45:56.931Z",
owner: "K7xTxu7PRDCuhFZRC",
updatedAt: "2017-09-06T20:45:56.931Z",
_id: "6gsb42PSSJt7PgsDG"
}, {
campaign: "Charles",
pany_ID: "1",
coreURL: ",",
createdAt: "2017-11-06T20:46:27.744Z",
owner: "K7xTxu7PRDCuhFZRC",
updatedAt: "2017-10-06T20:46:27.744Z",
_id: "Md4wCnEsrQrWApnLS"
}, {
campaign: "Gary",
pany_ID: "1",
coreURL: ",",
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"))
EDIT: However this only seems to group by "11" (todays Month)
Any help would be really appreciated
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",
pany_ID: "1",
coreURL: "http://www.test77.",
createdAt: "2017-11-06T20:45:56.931Z",
owner: "K7xTxu7PRDCuhFZRC",
updatedAt: "2017-09-06T20:45:56.931Z",
_id: "6gsb42PSSJt7PgsDG"
}, {
campaign: "Charles",
pany_ID: "1",
coreURL: "http://www.test66,",
createdAt: "2017-11-06T20:46:27.744Z",
owner: "K7xTxu7PRDCuhFZRC",
updatedAt: "2017-10-06T20:46:27.744Z",
_id: "Md4wCnEsrQrWApnLS"
}, {
campaign: "Gary",
pany_ID: "1",
coreURL: "http://www.test55,",
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"))
EDIT: However this only seems to group by "11" (todays Month)
Any help would be really appreciated
Share Improve this question edited Nov 15, 2017 at 8:12 Nick Wild asked Nov 15, 2017 at 8:06 Nick WildNick Wild 5751 gold badge12 silver badges27 bronze badges3 Answers
Reset to default 6datum.grouper
is undefined
so moment(datum.grouper)
is returning the current date.
You should instead be using datum[grouper]
or something like this _.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",
pany_ID: "1",
coreURL: "http://www.test77.",
createdAt: "2017-11-06T20:45:56.931Z",
owner: "K7xTxu7PRDCuhFZRC",
updatedAt: "2017-09-06T20:45:56.931Z",
_id: "6gsb42PSSJt7PgsDG"
}, {
campaign: "Charles",
pany_ID: "1",
coreURL: "http://www.test66,",
createdAt: "2017-11-06T20:46:27.744Z",
owner: "K7xTxu7PRDCuhFZRC",
updatedAt: "2017-10-06T20:46:27.744Z",
_id: "Md4wCnEsrQrWApnLS"
}, {
campaign: "Gary",
pany_ID: "1",
coreURL: "http://www.test55,",
createdAt: "2017-11-06T20:46:27.744Z",
owner: "K7xTxu7PRDCuhFZRC",
updatedAt: "2017-07-06T20:46:27.744Z",
_id: "5p44uiwRgqp35YXRf"
}, {
campaign: "Fred",
pany_ID: "1",
coreURL: "http://www.test55,",
createdAt: "2017-11-06T20:46:27.744Z",
owner: "K7xTxu7PRDCuhFZRC",
updatedAt: "2017-11-15T03:46:27.744Z",
_id: "5p44uiwRgqp35YXRf"
}, {
campaign: "Fred",
pany_ID: "1",
coreURL: "http://www.test55,",
createdAt: "2017-11-06T20:46:27.744Z",
owner: "K7xTxu7PRDCuhFZRC",
updatedAt: "2017-11-03T20:46:27.744Z",
_id: "5p44uiwRgqp35YXRf"
}, {
campaign: "Fred",
pany_ID: "1",
coreURL: "http://www.test55,",
createdAt: "2017-11-06T20:46:27.744Z",
owner: "K7xTxu7PRDCuhFZRC",
updatedAt: "2017-11-13T20:46:27.744Z",
_id: "5p44uiwRgqp35YXRf"
}, {
campaign: "Fred",
pany_ID: "1",
coreURL: "http://www.test55,",
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./ajax/libs/moment.js/2.19.2/moment.min.js"></script>
<script src="https://cdnjs.cloudflare./ajax/libs/lodash.js/4.17.4/lodash.min.js"></script>
Maybe you can use filter if you only need to current month, day, year .
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);
Or groupBy if you need all months (for all years)
const byMonth = _.groupBy(data, d => moment(d.updatedAt).month());
console.log(byMonth);
https://jsbin./xidido/edit?js,console
let groupedData = _
.chain(data)
.groupBy(record => moment(record.updatedAt).format("MM"))
.value();