Angular 为数组中的每个日期添加一次Typescript(多个日期)

Angular 为数组中的每个日期添加一次Typescript(多个日期),angular,typescript,Angular,Typescript,我有一个包含几个字段的对象数组。看起来是这样的: [ {"date": "01/01/2020", "location": "Main St", "customers": 100, "shoppers": 150, "visitors": 2000}, {"date": "01/01/2020", "location": "Lower St", "customers": 105, "shoppers": 166, "visitors": 2000}, {"date": "

我有一个包含几个字段的对象数组。看起来是这样的:

[
    {"date": "01/01/2020", "location": "Main St", "customers": 100, "shoppers": 150, "visitors": 2000},
    {"date": "01/01/2020", "location": "Lower St", "customers": 105, "shoppers": 166, "visitors": 2000},
    {"date": "01/01/2020", "location": "High St", "customers": 180, "shoppers": 260, "visitors": 2000},
    {"date": "02/01/2020", "location": "Main St", "customers": 156, "shoppers": 194, "visitors": 1566},
    {"date": "02/01/2020", "location": "Lower St", "customers": 80, "shoppers": 201, "visitors": 1566},
    {"date": "02/01/2020", "location": "High St", "customers": 97, "shoppers": 133, "visitors": 1566},
    {"date": "03/01/2020", "location": "Main St", "customers": 48, "shoppers": 97, "visitors": 1002},
    {"date": "03/01/2020", "location": "Lower St", "customers": 211, "shoppers": 287, "visitors": 1002},
    {"date": "03/01/2020", "location": "High St", "customers": 113, "shoppers": 233, "visitors": 1002}
]
如您所见,每天的访客数量是固定的(因为它已经是当天的总访客)

我将客户和购物者汇总如下:

this.customerCount = this.data.filter(item => item.date >= this.startDate && item.date <= this.endDate)
      .reduce((sum, current) => sum + current.customers, 0);

this.shopperCount = this.data.filter(item => item.date >= this.startDate && item.date <= this.endDate)
      .reduce((sum, current) => sum + current.shoppers, 0);

您应该做的第一件事是修复数据比较,我认为这种日期比较在所有情况下都不起作用,要么将日期格式转换为
yyy-mm-dd
或日期对象,然后进行日期比较

修复后,您可以使用Array.reduce通过以下方式获得访客总数:

const sum = this.data.reduce((acc, item) => 
    {
        if (item.date >= this.startDate && item.date <= this.endDate) {
            if (!acc.map[item.date.toString()]) {
                acc.map[item.date.toString()] = true;
                acc.sum += item.visitors;
            }
        }
        return acc;
    }, {map: {}, sum: 0}).sum;
const sum=this.data.reduce((acc,item)=>
{

如果(item.date>=this.startDate&&item.date,item.data指的是什么?@nimgwfc这是一个打字错误,“data”代替了“date”,很久以前就编辑过。这就是我的想法,谢谢!尽管我收到一个错误,item.date不能用作索引类型,因为它是Date@nimgwfc这是一个typescript警告,看起来你的日期无论如何都是字符串,但无论如何,上面的编辑应该会使警告消失。
const sum = this.data.reduce((acc, item) => 
    {
        if (item.date >= this.startDate && item.date <= this.endDate) {
            if (!acc.map[item.date.toString()]) {
                acc.map[item.date.toString()] = true;
                acc.sum += item.visitors;
            }
        }
        return acc;
    }, {map: {}, sum: 0}).sum;