Javascript 计算所提供年份和月份的对象数组中的数组数
假设我有一个对象数组,如下所示:Javascript 计算所提供年份和月份的对象数组中的数组数,javascript,Javascript,假设我有一个对象数组,如下所示: const timeValue = ["October 2020"] const information = [ {name: 'alpha',details: [ {"attachment": [123, 456], "receivedOn":"2020-10-24 04:05:16.000Z"}, {"attachment&quo
const timeValue = ["October 2020"]
const information = [
{name: 'alpha',details: [
{"attachment": [123, 456], "receivedOn":"2020-10-24 04:05:16.000Z"},
{"attachment": [1454, 1992], "receivedOn":"2020-10-24 04:05:16.000Z"}]},
{name: 'beta',details: [
{"attachment": ["12", 189] , "receivedOn":"2020-10-24 04:05:16.000Z"},
{"attachment": ["maggi", 1890, 2000], "receivedOn":"2021-02-24 04:05:16.000Z"},
{"attachment": [1990, 2001, 2901], "receivedOn":"2020-12-24 04:05:16.000Z"}]},
{name: 'theta',details: [
{"attachment": [1902, 1189] , "receivedOn":"2021-10-24 04:05:16.000Z"}] }];
我想获得给定时间值的alpha和beta计数,即预期O/p:{“alpha”:4,“beta”:2}
const result = information.reduce((acc, curr) => {
if (acc[curr.name] === undefined) acc[curr.name] = 0;
curr.details.forEach((d) => (acc[curr.name] += d.attachment.length));
return acc;
}, {});
为此,我尝试了,我能够得到每个名字的总附件数。即{alpha:4,beta:8,theta:2}
const result = information.reduce((acc, curr) => {
if (acc[curr.name] === undefined) acc[curr.name] = 0;
curr.details.forEach((d) => (acc[curr.name] += d.attachment.length));
return acc;
}, {});
console.log(result)
将输出作为{alpha:4,beta:8,theta:2}
const result = information.reduce((acc, curr) => {
if (acc[curr.name] === undefined) acc[curr.name] = 0;
curr.details.forEach((d) => (acc[curr.name] += d.attachment.length));
return acc;
}, {});
但我想知道2020年10月仅有的附件数量。有可能实现吗?如果有任何其他信息,请告诉我。,使用>,只需在您的forEach中添加条件,如果您只对年-月比较感兴趣,您可以这样做:
var result = information.reduce((acc, curr) => {
if (acc[curr.name] === undefined) acc[curr.name] = 0;
curr.details.forEach((d) => {
var receivedOn = new Date(d.receivedOn);
acc[curr.name] += (receivedOn.getFullYear() == 2020 && receivedOn.getMonth() == 9 ? d.attachment.length : 0)});
return acc;
}, {});
timeValue
的可能值是什么?该数组中可以有多个值吗?不,timeValue中只能有一个元素。您的问题与此非常相似:即使是数组内容也是如此。你知道吗?谢谢,这正是我需要的