我有一个多重搜索字段。我希望可以循环遍历一系列属性并过滤掉它对应的正确数据,并在表格中显示数据,但我以不同的方式工作。请查看旧代码和新代码。新代码不起作用,新代码用于循环以匹配属性,并且它们应用了正确的过滤器。每个项目都会点击.filter()函数。因此9个项目将对其进行9次以对每个项目进行过滤。这是一个可重用的组件。
旧代码
// filter is a get and set, depending on the items it will run this code; I have 9 items and each item will hit .filter(item)
.filter((item: Array<any>) => {
// item looks like this:
item = {"description": "item name", "unit": "56", "ourPrice": 200, "notes": "lorem", "seller": "lorem ipsum", "manufacturer":"lorem ipsum"};
let SelectedItem = item.description.toLowerCase();
// this.filter is an array of values i type in the search field
// I do this for each property in its own .filter() and it works and if it is number value I do it based on number .
return SelectedItem.indexOf(this.filter.description.toLowerCase());
}).filter((item: Array<any>) => {
// add so on
});
新代码
.filter((item: Array<any>) => {
var _item;
// this returns just a string of values but this is only shown here for visual but not written here
this._sortProperties = {"description", "unit", "ourPrice", "notes", "seller", "manufacturer" };
for(let _i = 0; _i < this._sortProperties.length; _i++) {
let _filterData = item[this._sortProperties[_i]];
if(typeof _filterData === 'string' || _filterData instanceof String) {
_item = _filterData.toLowerCase();
_item.indexOf(this.filter[this._sortProperties[_i]].toLowerCase()) !== -1;
} else if(isNaN(_filterData) === true) {
_item = _filterData;
_item !== -1;
}
}
return _item;
});