Angular 如何使用具有特定日期格式的分组键的分组管道

Angular 如何使用具有特定日期格式的分组键的分组管道,angular,typescript,Angular,Typescript,我有一个数据列表: players = [{name: 'Gene', team: 'team alpha', date: '2019-09-18T16:45:42' }, {name: 'Steve', team: 'team gamma', date: '2019-09-18T15:45:42'}, {name: 'George', team: 'team beta', date: '2019-20-18T12:45:42'},

我有一个数据列表:

  players  = [{name: 'Gene', team: 'team alpha', date: '2019-09-18T16:45:42' },
              {name: 'Steve', team: 'team gamma', date: '2019-09-18T15:45:42'},
              {name: 'George', team: 'team beta', date: '2019-20-18T12:45:42'},
              {name: 'Paula', team: 'team beta', date: '2019-18-18T15:45:42'},
              {name: 'Jhon', team: 'team gamma', date: '2019-09-18T15:45:42'}];
}

我希望使用仅在日期上的分组管道,而不是“2019-09-18T15:45:42”格式,它应仅在以下格式“2019-09-18”上进行,该格式更符合分组

这是使用的管道:

  @Pipe({name: 'groupBy'})
   export class GroupByPipe implements PipeTransform {
    transform(collection: Array<any>, property: string): Array<any> {
     if(!collection) {
        return null;
     }

    const groupedCollection = collection.reduce((previous, current)=> {
        if(!previous[current[property]]) {
            previous[current[property]] = [current];
        } else {
            previous[current[property]].push(current);
        }

        return previous;
    }, {});

    return Object.keys(groupedCollection).map(key => ({ key, value: groupedCollection[key] }));
  }
}

我正在听取您的解决方案建议

您可以在对收藏进行分组之前转换收藏日期:

  @Pipe({name: 'groupBy'})
   export class GroupByPipe implements PipeTransform {
    transform(collection: Array<any>, property: string): Array<any> {
     if(!collection) {
        return null;
     }

    const mappedCollection = collection.map(player => ({
                               ...player, 
                               date: player.date.split('T')[0]
                             }));

    const groupedCollection = mappedCollection.reduce((previous, current)=> {
        if(!previous[current[property]]) {
            previous[current[property]] = [current];
        } else {
            previous[current[property]].push(current);
        }

        return previous;
    }, {});

    return Object.keys(groupedCollection).map(key => ({ key, value: groupedCollection[key] }));
  }
}
@Pipe({name:'groupBy'})
导出类GroupByPipe实现PipeTransform{
转换(集合:数组,属性:字符串):数组{
如果(!集合){
返回null;
}
const mappedCollection=collection.map(player=>({
…玩家,
日期:player.date.split('T')[0]
}));
const groupedCollection=mappedCollection.reduce((上一个,当前)=>{
如果(!上一个[当前[属性]]){
上一个[当前[属性]]=[当前];
}否则{
上一个[当前[属性]]。推送(当前);
}
返回上一个;
}, {});
返回Object.keys(groupedCollection.map)(key=>({key,value:groupedCollection[key]}));
}
}

您的解决方案中有一个语句错误,我通过以下方式更改了您的解决方案:const mappedCollection=collection.map(elt=>({…elt,date:elt.date.split('T')[0]}))
   date.split('T')[0];
  @Pipe({name: 'groupBy'})
   export class GroupByPipe implements PipeTransform {
    transform(collection: Array<any>, property: string): Array<any> {
     if(!collection) {
        return null;
     }

    const mappedCollection = collection.map(player => ({
                               ...player, 
                               date: player.date.split('T')[0]
                             }));

    const groupedCollection = mappedCollection.reduce((previous, current)=> {
        if(!previous[current[property]]) {
            previous[current[property]] = [current];
        } else {
            previous[current[property]].push(current);
        }

        return previous;
    }, {});

    return Object.keys(groupedCollection).map(key => ({ key, value: groupedCollection[key] }));
  }
}