[英]Filter data with dynamic and nested filter conditions using Lodash
我想用多个以及动态过滤条件过滤我的产品数据。 首先我的原始数据格式是:
[{
...,
"_source": {
...,
"categories": [
"home",
"office"
],
...,
"attributes": {
"colors": [
"Red-White-Blue",
"Orange-Red-Black"
]
},
...,
}
}]
现在,我有另一个动态对象,其键值对在运行时生成。 对象是:
{
selectedFilters: {
categories: ["home", "office", "public"],
colors: ["Red-White-Blue", "Orange-Red-Black", "Blue"],
...
}
}
这里,类别数组可能存在,也可能不存在。 同样,keyvalue对是动态生成的。
现在,我想循环遍历selectedFilters
对象中的所有键,并从原始数据中找到它们各自的数组。
我们说,如果selectedFilters包含categories
为home
和colors
包括white
,然后所有家庭的产品category
和白色colors
应该从主要产品的数据返回。 如果colors
没有值,那么它应该返回所有category
为home的产品数据,同样如果color
为白色且categories
为空,则无论何种类别,它都应返回所有白色产品。
无论有没有Lodash,我怎样才能做到这一点?
以下代码适用于searchFilters中的动态键。 然后,我正在过滤产品数据中的数据。
let keys = Object.keys(this.selectedFilters);
keys.forEach(function(filterKey) {
let filterdProducts = _.filter(self.products, function(o) {
let flg = false;
let searchFilterKey = self.selectedFilters[filterKey];
searchFilterKey.forEach(function(sKey) {
let ind = o._source[filterKey].indexOf(sKey)
if (ind > 0)
flg = true;
})
if (flg)
return o;
});
self.filteredProducts = filterdProducts
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.