0

我正在尝试过滤来自可观察对象的数组,如果属性“start”是日期字符串与另一个日期字符串匹配,但在过滤器运算符函数中,参数未将“start”识别为有效属性

我已经阅读了多个示例,并且与我的比较似乎是一个非常相似的场景

export class AuctionsService() {

private _auctions = new BehaviorSubject(
    [
      {
        idAuction: '1',
        start: '2019-07-18T15:30',
      },
      {
        idAuction: '2',
        start: '2019-07-18T15:30',
      },
      {
        idAuction: '3',
        start: '2019-07-18T15:30',
      },
      {
        idAuction: '4',
        start: '2019-07-19T15:30',
      },
    ]
  );
 get auctions() {
    return this._auctions.asObservable();
  }
}

当我订阅这个 observable 时在另一个文件上

    selectedDate = '2019-07-19T12:46';

  constructor(
    private auctionsService: AuctionsService,
  ) { }

      ngOnInit() {
        this.auctionsService.auctions.pipe(filter(
          aucs => aucs.start.slice(0, 10) === this.selectedDate.slice(0, 10)
        )).subscribe(
          aucs => { this.auctions = aucs; }
        );
      }

预期输出应为:{ idAuction: '4', start: '2019-07-19T15:30', },

但没有编译它声明 .start 不是 aucs 的有效属性

4

1 回答 1

0

正如@dcg 所说,aucsthis.auctionsService.auctionsobservable 返回的是一个数组,但是您的代码将其视为数组中的一个元素。因此,您需要将其过滤为数组:

ngOnInit() {
  this.auctionsService.auctions.pipe(
    map(
      aucs => aucs.filter(auc => auc.start === this.selectedDate;
      )
    )
  ).subscribe(aucs => {
    this.auctions = aucs;
  });
}
于 2019-07-20T15:24:04.743 回答