首页 > 解决方案 > ... 在对象数组之前的目的是什么

问题描述

我目前正在学习 Angular,并且发现了对我来说有点神秘的代码示例。

我有一个返回Observable<Product[]>对象数组的函数:

  connect(): Observable<Product[]> {

    const dataMutations = [
      this.productsSubject,
      this.paginator.page,
      this.sort.sortChange
    ];

    return merge(...dataMutations).pipe(map((products) => {
      this.paginator.length = products.length;
      return this.getPagedData(this.getSortedData([...products]));
    }));
  }

在这个代码块中,有一个函数getSortedData可以获取产品数组之前的[...products]用途是什么?...

的代码示例getSortedData

private getSortedData(data: Product[]) {
    if (!this.sort.active || this.sort.direction === '') {
      return data;
    }

    return data.sort((a, b) => {
      const isAsc = this.sort.direction === 'asc';
      switch (this.sort.active) {
        case 'title': return compare(a.title, b.title, isAsc);
        default: return 0;
      }
    });
  }

标签: angulartypescriptangular7

解决方案



推荐阅读