好的,所以我有一个对象数组,这是我要过滤的数据:
const posts = [
{
hairday: '2',
products: ['product1', 'product2', 'product3', 'product4'],
rating: '2',
drying: 'diffuser'
},
{
hairday: '2',
products: ['product6', 'product7', 'product8', 'product9'],
rating: '4',
drying: 'air dry'
},
{
hairday: '3',
products: ['product10', 'product11', 'product13', 'product14'],
rating: '3',
drying: 'air dry'
},
{
hairday: '4',
products: ['product15', 'product26', 'product37', 'product14'],
rating: '5',
drying: 'towel dry'
},
]
我想按多种条件过滤以上数据。这是条件的示例对象:
{
products: ['product1', 'product13'],
hairday: ['2', '3'],
drying: ['air dry', 'diffuser'],
rating: []
}
所以我想获得所有与每个数组中至少一项匹配的post
对象。
因此,过滤后的物品应具有product1或product13和hairday 2或hairday 3并干燥风干或干燥扩散器和任何等级。
解决此问题的最佳方法是什么?筛选器对象的结构是否最佳? 在此先感谢<3
答案 0 :(得分:1)
对于不需要手动列出键的更健壮的解决方案,可以使用Object.keys()
遍历条件对象,然后将post
数组中的各个对象与其进行比较:< / p>
const filtered = posts.filter(post => {
const conditionKeys = Object.keys(conditions);
const fulfillments = conditionKeys.map(k => {
const condition = conditions[k];
// If we encounter an empty array, then criteria is always met
if (condition.length === 0) {
return true;
}
// Enforces that as long as ONE subcondition is met (`OR`)
return condition.filter(v => post[k].includes(v)).length > 0;
});
// Enforce that EVERY condition must be met (`AND`)
// A condition is considered met as long as it is true
return fulfillments.every(x => x);
});
在遍历各个条件(工作日,产品等)时,我们只想检查您的对象是否包含一个或多个列出的值(即两个数组中的任何一个都相交)。如果存在交叉点,则长度将> 0,否则将为0。
请参见下面的概念验证:
const posts = [{
hairday: '2',
products: ['product1', 'product2', 'product3', 'product4'],
rating: '2',
drying: 'diffuser'
},
{
hairday: '2',
products: ['product6', 'product7', 'product8', 'product9'],
rating: '4',
drying: 'air dry'
},
{
hairday: '3',
products: ['product10', 'product11', 'product13', 'product14'],
rating: '3',
drying: 'air dry'
},
{
hairday: '4',
products: ['product15', 'product26', 'product37', 'product14'],
rating: '5',
drying: 'towel dry'
},
];
const conditions = {
products: ['product1', 'product13'],
hairday: ['2', '3'],
drying: ['air dry', 'diffuser'],
rating: []
};
const filtered = posts.filter(post => {
const fulfillments = Object.keys(conditions).map(k => {
const condition = conditions[k];
// If we encounter an empty array, then criteria is always met
if (condition.length === 0) {
return true;
}
return condition.filter(v => post[k].includes(v)).length > 0;
});
return fulfillments.every(x => x);
});
console.log(filtered);
答案 1 :(得分:0)
我可以这样写一个相当通用的版本:
const findMatches = (filters, xs) =>
xs .filter (x => Object .entries (filters) .every (([name, vals]) =>
vals.length == 0 ||
(Array .isArray (x [name])
? x [name] .some (val => vals .includes (val))
: vals .includes (x [name]))
))
const posts = [{hairday: '2', products: ['product1', 'product2', 'product3', 'product4'], rating: '2', drying: 'diffuser'}, {hairday: '2', products: ['product6', 'product7', 'product8', 'product9'], rating: '4', drying: 'air dry'}, {hairday: '3', products: ['product10', 'product11', 'product13', 'product14'], rating: '3', drying: 'air dry'}, {hairday: '4', products: ['product15', 'product26', 'product37', 'product14'], rating: '5', drying: 'towel dry'}]
const filters = {products: ['product1', 'product13'], hairday: ['2', '3'], drying: ['air dry', 'diffuser'], rating: []}
console .log (findMatches (filters, posts))
.as-console-wrapper {min-height: 100% !important; top: 0}
代码对帖子,产品,工作日等一无所知。它只是根据条件对象过滤对象列表。
但是,我对此不满意。空数组(ratings
)的特殊处理使人感觉这是一个错误的设计。我建议,如果您没有任何匹配的值,则只需在条件对象中不包含它。因此,我们将删除“ ratings1”以获得
const conditions = {
products: ['product1', 'product13'],
hairday: ['2', '3'],
drying: ['air dry', 'diffuser'],
};
对我来说,从其他条件来看,从本质上讲,这更合乎逻辑,即对象中的值必须是给定列表之一(或者在数组的情况下,元素之一必须是给定列表之一)给定列表)。空的ratings
对象可以解决这个问题。
这会将代码稍微简化为
const findMatches = (filters, xs) =>
xs .filter (x => Object .entries (filters) .every (([name, vals]) =>
Array .isArray (x [name])
? x [name] .some (val => vals .includes (val))
: vals .includes (x [name])
))
这些版本的效率低下。我们为每个x [name]
获取两次x
,这可能没什么大不了的,但是我们也为每个值在Object .entries
上调用了filters
。如果该功能成为您应用程序中的瓶颈,则您可能需要修复它们。此版本可以清理它们,还可以使您在给定的条件下创建可重用的功能:
const findMatches = (filters) => {
const toMatch = Object .entries (filters)
return (xs) =>
xs .filter (x => toMatch .every (([name, vals]) => {
const val = x [name]
return Array .isArray (val)
? val .some (val => vals .includes (val))
: vals .includes (val)
}))
}
您可能会略有不同:
const myFilter = findMatches (filters)
// ... later
console .log (myFilter (posts))
但是我不会打扰的,除非事实证明这是应用程序的瓶颈,这似乎不太可能。第一个版本(最好是第二个版本)要干净得多。