根据Javascript中的日期计算数组中对象的数量
我有一个根据Javascript中的日期计算数组中对象的数量,javascript,Javascript,我有一个reportsListinclude 1000对象报告,如下所示: { "id": 39, "userId": 3, "emotion": { "id": "worried", "name": "Worried Face", "colons": ":worried:", "emoticons": [], "nati
reportsList
include 1000对象报告,如下所示:
{
"id": 39,
"userId": 3,
"emotion": {
"id": "worried",
"name": "Worried Face",
"colons": ":worried:",
"emoticons": [],
"native": "You can try:
// untested
// desired months
const months = [6, 7, 8, 9];
// For each of the desired month
const emotions = months.map(month => {
// Filter out the same month object
const array = reportsList.filter(obj => {
// Get the month per data
const objMonth = new Date(obj.date).getMonth() + 1;
// return true if it is the same month
return month === objMonth;
});
// return the array length
return array.length;
});
{
“id”:39,
“用户ID”:3,
“情感”:{
“id”:“担心”,
“名字”:“忧愁的脸”,
“科隆”:“担心:”,
“表情符号”:[],
“本机”:“您可以尝试:
const emoticonsByMonth = reportsList.reduce((acc, report) => {
const month = new Date(report.date).getMonth();
if (month >= 6 && month <= 9) acc[month - 5] += 1;
return acc;
}, [0, 0, 0, 0]);
我猜是这样的:
const emoticsonsbymonth=reports列表减少((acc,报告)=>{
const month=新日期(report.Date).getMonth();
如果(month>=6&&month)没有months数组可以实现这一点吗?当然可以!const emotions=[6,7,8,9].map(month=>reportsList.filter(obj=>month==new Date(obj.Date).getMonth()+1.length);
@Nina Scholz抱歉,我被编辑了。