Angular 2/Typescript:筛选数组未返回预期结果
在filterProducts()函数中,我有两个条件测试。如果找到传递给函数的类型的产品,并且该产品按预期工作,则以下内容将对产品进行筛选:Angular 2/Typescript:筛选数组未返回预期结果,angular,typescript,Angular,Typescript,在filterProducts()函数中,我有两个条件测试。如果找到传递给函数的类型的产品,并且该产品按预期工作,则以下内容将对产品进行筛选: if (filterCriteria.type) { filteredProducts = filteredProducts.filter(product => product.producttype == filterCriteria.type); } 第二个测试应该根据传递给函数的类别过滤产品: if (filterCrite
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 ");
}
});
}
代码中的log语句显示,是的,有传递到函数中的类别的产品,但在最后,应该保存找到的产品的filteredProducts数组不包含任何数据
这两段几乎完全相同。一个有效,另一个无效
以下是整个功能:
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;
}
在此代码段中:
if (filterCriteria.type) {
filteredProducts = filteredProducts.filter(product =>
product.producttype == filterCriteria.type); // <- Implicit return of this value to the filter function.
}
第二组代码是多行代码,因此需要显式返回语句
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) => {
console.log(product.productName);
return product.productName.toLocaleLowerCase().indexOf(filterBy) !== -1; // <-- Inner return returns for this ONE item within the filter function
})
}
performFilter(filterBy:string):IPProduct[]{
filterBy=filterBy.toLocaleLowerCase();
返回此.products.filter((产品:IPProduct)=>{
console.log(product.productName);
在此代码段中返回product.productName.toLocaleLowerCase().indexOf(filterBy)!==-1;//:
if (filterCriteria.type) {
filteredProducts = filteredProducts.filter(product =>
product.producttype == filterCriteria.type); // <- Implicit return of this value to the filter function.
}
第二组代码是多行代码,因此需要显式返回语句
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) => {
console.log(product.productName);
return product.productName.toLocaleLowerCase().indexOf(filterBy) !== -1; // <-- Inner return returns for this ONE item within the filter function
})
}
performFilter(filterBy:string):IPProduct[]{
filterBy=filterBy.toLocaleLowerCase();
返回此.products.filter((产品:IPProduct)=>{
console.log(product.productName);
返回product.productName.toLocaleLowerCase().indexOf(filterBy)!=-1;//我知道SOP是指如果你投了反对票,就什么也不说……但是作为一个相对较新的人,我很感兴趣为什么会投反对票。这似乎是一个完全正确和完整的问题?我知道SOP是指如果你投反对票,就什么也不说……但是作为一个相对较新的人,我很感兴趣为什么会投反对票。Th是否似乎是一个完全有效且完整的问题?我不希望在仅执行一个段后返回。我希望先根据类别进行筛选,然后根据类型进行筛选,并在函数末尾返回结果filteredproducts数组。如果我要在每个段中包含return语句(如果我理解正确的话)一次只能执行函数的一段,这不是我想要的结果。arrow函数中的return语句返回到该函数。因此在本例中,它们返回到filter方法,以便它可以处理下一段。您的另一个选项是只需删除所有日志语句,即可返回到单行函数带有默认返回值的离子。:-)谢谢,Deborah,我会尝试你的建议。但我认为代码段以前只包含一条语句,它仍然没有产生正确的结果。我只是出于测试目的添加了日志语句。我将删除它们并再次测试。如果是这样,则说明其他一些错误。如果是这样,请尝试注释第二条语句(filterCriter.type)要查看此筛选器是否在没有第二个筛选器的情况下工作。我删除了日志语句并添加了返回值。它像一个符咒一样工作。谢谢你,Deborah。我不希望在仅执行一个段后返回。我希望先基于类别进行筛选,然后基于类型进行筛选,并在函数末尾返回生成的filteredproducts数组.如果我在每个段中包含返回语句(如果我正确理解您)一次只能执行函数的一段,这不是我想要的结果。arrow函数中的return语句返回到该函数。因此在本例中,它们返回到filter方法,以便它可以处理下一段。您的另一个选项是只需删除所有日志语句,即可返回到单行函数带有默认返回值的离子。:-)谢谢,Deborah,我会尝试你的建议。但我认为代码段以前只包含一条语句,它仍然没有产生正确的结果。我只是出于测试目的添加了日志语句。我将删除它们并再次测试。如果是这样,则说明其他一些错误。如果是这样,请尝试注释第二条语句(filterCriter.type)以查看此筛选器在没有第二个筛选器的情况下是否工作。我删除了日志语句并添加了返回。它工作得很好。谢谢你,黛博拉。