在多个字段上进行数组过滤 [英] Array filtering on multiple fields
本文介绍了在多个字段上进行数组过滤的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有这段代码可以过滤我的数据.我问我,是否有一种方法不显式过滤每个字段(id
,mandant
,zonenlogik
...).
也许有一种更平滑的方法可以在所有字段上设置过滤器,而无需显式调用它们?
I have this code, which filters my data. I'm asking me, if there is a way not to filter each field (id
, mandant
, zonenlogik
...) explicitly.
Maybe there is a more smooth way to set the filter on all fields without calling them explicitly?
let filteredList = this.state.freights.filter((freight) => {
if (freight.id.toLowerCase().indexOf(this.state.search.toLowerCase()) !== -1) {
return freight;
}
if (freight.mandant.toLowerCase().indexOf(this.state.search.toLowerCase()) !== -1) {
return freight;
}
if (freight.zonenlogik.toLowerCase().indexOf(this.state.search.toLowerCase()) !== -1) {
return freight;
}
if (freight.frachtart_nr.toLowerCase().indexOf(this.state.search.toLowerCase()) !== -1) {
return freight;
}
if (freight.transportart_nr.toLowerCase().indexOf(this.state.search.toLowerCase()) !== -1) {
return freight;
}
if (freight.spedit_nr.toLowerCase().indexOf(this.state.search.toLowerCase()) !== -1) {
return freight;
}
if (freight.spedit2_nr.toLowerCase().indexOf(this.state.search.toLowerCase()) !== -1) {
return freight;
}
if (freight.lager_nr.toLowerCase().indexOf(this.state.search.toLowerCase()) !== -1) {
return freight;
}
});
推荐答案
您可以使用Object.values()
获取对象值,然后遍历这些值以检查字符串中是否存在子字符串,然后返回已过滤的对象>
You can fetch the object values using Object.values()
and then loop over those to check whether a substring is present in the string and then return the filtered object
let filteredList = this.state.freights.filter((freight) => {
let search = this.state.search.toLowerCase();
var values = Object.values(freight);
var flag = false
values.forEach((val) => {
if(val.toLoweCase().indexOf(search) > -1) {
flag = true;
return;
}
}
if(flag) return freight
});
这篇关于在多个字段上进行数组过滤的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文