前端——JS实现多条件过滤数组
通过js实现多条件过滤
//多条件过滤,array是传入的数据,filters是过滤条件
multiFilter(array, filters) {
filters = me.evtHandlers.removeEmpty(filters);
const filterKeys = Object.keys(filters)
return array.filter((item) => {
return filterKeys.every(key => {
if (!filters[key].length) return true
return !!~filters[key].indexOf(item[key])
})
})
},
//去掉过滤条件值空的键
removeEmpty(obj) {
obj = JSON.parse(JSON.stringify(obj))
for (let k in obj) {
v = obj[k];
if (v === '') {
delete obj[k];
} else if (v.constructor == Object) {
me.evtHandlers.removeEmpty(v);
}
}
return obj
},