基于空值的Javascript过滤
我正在筛选这个列表基于空值的Javascript过滤,javascript,arrays,Javascript,Arrays,我正在筛选这个列表 [ { appLearningItemId: 67 catalogues: (2) [ {id: 1041, value: "New Catalog"}, {id: 1058, value: "Test"}] categories: (3) [{id: 1, value: "Soft Skills"}, {id: 3, value: &
[
{
appLearningItemId: 67
catalogues: (2) [ {id: 1041, value: "New Catalog"},
{id: 1058, value: "Test"}]
categories: (3) [{id: 1, value: "Soft Skills"},
{id: 3, value: "Non-technical"},
{id: 5, value: "Induction"}]
code: "CCE0013"
suppliers: (3) [{id: 1, value: "Company XYZ Ltd"},
{id: 2, value: "test c2"},
{id: 17, value: "new company"} ]
title: "07 Values & Beliefs"
type: {id: 11377, value: "Elearning"}
}, ... * 682 items
]
使用此对象和过滤器
const filters = {
type: 'Elearning',
catalog: 1041,
category: 1,
supplier: 1
}
let advancedFilteredLearningItems = this.originalLearningItems.filter(obj => obj.type.value == filters.type
&& obj.catalogues.some( catalogs => catalogs.id == filters.catalog)
&& obj.categories.some( category => category.id == filters.category)
&& obj.suppliers.some(supplier => supplier.id === filters.supplier));
console.log(advancedFilteredLearningItems)
这很有效。有时,筛选器对象的某些值或最多3个值中会有空值,例如:
const filters = {
type: 'Elearning',
catalog: null,
category: 1,
supplier: null
}
我如何编辑过滤代码,使其不过滤空项目,以便在这种情况下,我可以返回类别为1的所有电子学习项目?当前筛选器代码正在查找空值,但我只想将其从筛选器中完全忽略。向所有筛选器添加空检查是否有效?因此,如果为null,则返回true,因为您希望所有目录都在它之前?这不是最有效的解决方案,但它应该能让您到达需要的地方
const filters = {
type: 'Elearning',
catalog: 1041,
category: null,
supplier: null
}
let advancedFilteredLearningItems = this.originalLearningItems.filter(obj =>
(filters.type != null ? obj.type.value == filters.type : true)
&& obj.catalogues.some(catalogs => filters.catalog != null ? catalogs.id == filters.catalog : true)
&& obj.categories.some(category => filters.category != null ? category.id == filters.category : true)
&& obj.suppliers.some(supplier => filters.supplier != null ? supplier.id === filters.supplier : true));
console.log(advancedFilteredLearningItems)
编辑:
一个更有效的解决方案是,如果没有过滤器,就不要在目录中循环。这可以通过添加一个额外的函数来实现
let advancedFilteredLearningItems = this.originalLearningItems.filter(obj =>
(filters.type != null ? obj.type.value == filters.type : true)
&& this.filterItems(filters.catalog, obj.catalogs)
&& this.filterItems(filters.categories, obj.categories)
&& this.filterItems(filters.supplier , obj.suppliers)
console.log(advancedFilteredLearningItems)
// new function
filterItems(filter, array) {
if (filter == null) {
return true;
}
return array.some(item => item.id == filter);
}
编辑2:如果你不想添加新函数,这应该和我的第一次编辑一样有效
let advancedFilteredLearningItems = this.originalLearningItems.filter(obj =>
(filters.type == null || obj.type.value == filters.type)
&& (filters.catalog == null || obj.catalogues.some(catalogs => catalogs.id == filters.catalog))
&& (filters.categories == null || obj.categories.some(category => category.id == filters.category))
&& (filters.supplier == null || obj.suppliers.some(supplier => supplier.id == filters.supplier))
);
如果第一个不起作用,那么我认为第二个不会起作用。。。我会继续调查的it@Bwizard我更新了我的代码。在我的函数中我需要一些返回值,非常感谢。如果我能加倍投票,我会的!您可以将三元运算符替换为OR
(filters.type==null | | obj.type.value==filters.type)
@pilchard这是一个很好的观点。我更新了第二次编辑以反映这一点