Angular 2 / Typescript:过滤数组不返回预期结果

时间:2017-08-28 19:44:32

标签: angular typescript

在我的filterProducts()函数中,我有两个条件测试。如果找到传入函数的类型的产品并且它按预期工作,则以下过滤产品:

if (filterCriteria.type) {
  filteredProducts = filteredProducts.filter(product => 
     product.producttype == filterCriteria.type);
}

第二个测试应该根据传递给函数的类别过滤产品:

if (filterCriteria.category) {

  console.log("ProductService.filterCriteria.category = " + filterCriteria.category);

  filteredProducts = filteredProducts.filter(product => 
    {
      console.log("ProductService.product.category = " + product.category);

      product.category == filterCriteria.category;

      if(product.category == filterCriteria.category) {
        console.log("ProductService.product.category = filterCriteria.category ");
      }
    });
}

代码中的日志语句显示,是的,有类别的产品传递到函数中,但最后filterProducts数组应该包含找不到的数据。

两个部分几乎完全相同。一个有效,另一个没有。

以下是整个功能:

  public filterProducts(filterCriteria: FilterCriteria): Product[] {
    let filteredProducts = this.products;
    let tmp = this.products;


    if (filterCriteria.category) {

      console.log("ProductService.filterCriteria.category = " + 
          filterCriteria.category);

      filteredProducts = filteredProducts.filter(product => 
        {
          console.log("ProductService.product.category = " + 
              product.category);

          product.category == filterCriteria.category;

          if(product.category == filterCriteria.category) {
            console.log("ProductService.product.category = 
                filterCriteria.category ");
          }
        });
    }

    console.log("filteredProducs 1.length = " + filteredProducts.length)

    if (filterCriteria.type) {
      filteredProducts = filteredProducts.filter(product => 
          product.producttype == filterCriteria.type);
   }

    console.log("filteredProducs.length = " + filteredProducts.length)
    return filteredProducts;
  }

1 个答案:

答案 0 :(得分:2)

在此代码段中:

if (filterCriteria.type) {
  filteredProducts = filteredProducts.filter(product => 
     product.producttype == filterCriteria.type); // <- Implicit return of this value to the filter function.
}

箭头功能中只有一行。因此,默认情况下,它将其用作return语句。所以你上面的代码基本上是这样的捷径:

if (filterCriteria.type) {
  filteredProducts = filteredProducts.filter(product => 
     return (product.producttype == filterCriteria.type));
}

第二组代码是多行,因此您需要一个显式的return语句。

if (filterCriteria.category) {

  console.log("ProductService.filterCriteria.category = " + filterCriteria.category);

  filteredProducts = filteredProducts.filter(product => 
    {
      console.log("ProductService.product.category = " + product.category);

      if(product.category == filterCriteria.category) {
        console.log("ProductService.product.category = filterCriteria.category ");
      }

      return product.category == filterCriteria.category;  //<-- RETURN
    });
}

例如,这是我的代码中的一个。这两者在功能上都是相同的。

隐含退货的单行箭头功能:

performFilter(filterBy: string): IProduct[] {
    filterBy = filterBy.toLocaleLowerCase();
    return this.products.filter((product: IProduct) =>
          product.productName.toLocaleLowerCase().indexOf(filterBy) !== -1);
}

带有显式返回的多行箭头功能

performFilter(filterBy: string): IProduct[] {
    filterBy = filterBy.toLocaleLowerCase();
    return this.products.filter((product: IProduct) => {
        console.log(product.productName);
        return product.productName.toLocaleLowerCase().indexOf(filterBy) !== -1;  // <-- Inner return returns for this ONE item within the filter function
    })
}