我有一个过滤特定项目表格的功能。在我的应用程序中,我有许多具有不同过滤器的表,因此我编写了一个函数,可以用动态数据过滤表。一些过滤器使用输入,用户必须在其中输入查询。如果用户的查询仅部分匹配表数组之一中的字符串,则该表不会返回预期结果。
例如
filterArr = [null, null, "246", null]
TableFilter = [
{"name": Matthew Shapiro", "claim_number":"2469135-6", "type": "Medical", "date": "05/04/2018", "doctor": "John Smith", "hospital": "University Hospital"},
{"name": "John Connor", "claim_number": "1352460-2", "type": "Medical", "date": "01/01/2018", "doctor": "John Smith", "hospital": "University Hospital"},
{"name": "Sam Heist", "claim_number": "1270120-9", "type": "Medical", "date": "02/11/2018", "doctor": "William Rodgers", "hospital": "Springfield Hospital"}
]
在此示例中,即使在“ 2469135-6”和“ 1352460-2”中找到246,该表也不会返回前两行。如果我将过滤器中的查询更新为任意一个数字的完整字符串,它将得到。
如果filterArr中的字符串与TableFilter [i]中的字符串部分匹配,如何更新我的函数以返回适当的TableFilter?
下面是我的功能以及其中一个表的屏幕截图。
let rowArr = []
let filteredResults = []
this.rows.map(element => {
rowArr = Object.values(element); // Object.values of each row
rowArr.filter(e => {
if (rowArr.indexOf(filterArr) !== -1) {
// check for substring match. Currently not working
console.log('match');
}
if (filterArr.includes(e)) { // check if filter has value that matches row
filteredResults.push(element);
this.rows = filteredResults;
}
})
})
答案 0 :(得分:1)
您要对表过滤器数组进行过滤,其中行值中的 some 包含filterArray的值 some 。
var filterArr = [null, null, "246", null];
var TableFilter = [
{"name": "Matthew Shapiro", "claim_number":"2469135-6", "type": "Medical", "date": "05/04/2018", "doctor": "John Smith", "hospital": "University Hospital"},
{"name": "John Connor", "claim_number": "1352460-2", "type": "Medical", "date": "01/01/2018", "doctor": "John Smith", "hospital": "University Hospital"},
{"name": "Sam Heist", "claim_number": "1270120-9", "type": "Medical", "date": "02/11/2018", "doctor": "William Rodgers", "hospital": "Springfield Hospital"}
];
var foundResults = TableFilter.filter(row => {
return Object.keys(row).some(fieldName => {
return filterArr.some(filt => {
return row[fieldName].indexOf(filt) > -1;
});
});
});
console.log(foundResults);