[英]Angular custom Filter pipe after KeyValue pipe
I'm trying to implement a custom filter after using Angular's inbuilt Key Value pipe我正在尝试使用 Angular 的内置键值管道后实现自定义过滤器
I have an array with values for example例如,我有一个带有值的数组
object= [
{ key: "Added_By", value: "Yi" },
{ key: "Assigned_To", value: "-" },
{ key: "Bought_Date", value: 43810 },
{ key: "Brand", value: "Samsung PM863" },
{ key: "Capacity", value: 3.84 },
]
I want to filter based on multiple incoming values but incoming values vary for example我想根据多个传入值进行过滤,但传入值会有所不同,例如
It Could be 1 key/Value它可能是 1 个键/值
Filter= [{ key: "Added_By", value: "Yi" }]// Return Object
or multiple或多个
Filter= [{ key: "Added_By", value: "Yi" }, { key: "Bought_Date", value: 43810 }] //both matches return object
Filter= [{ key: "Added_By", value: "ABC" }, { key: "Bought_Date", value: 43810 }] //1 match but 1 doesn't return false
I want to return object if all the conditions are met如果满足所有条件,我想返回对象
For a single key/value I tried对于我尝试过的单个键/值
let Key= Filter[0].key
let Value=Filter[0].value
let KeyFilter = object.filter(x => x.key === Key)
if (KeyFilter[0].value.toString().toLowerCase().indexOf(Value.toLowerCase()) !== -1)
return items
But this approach only works only when 1 object is present in filter array但是这种方法仅在过滤器数组中存在 1 个对象时才有效
I've created a function that accepts an array of objects , and an object with key: value pairs .我创建了一个函数,它接受一个对象数组,以及一个带有键值对的对象。
The keys of objects in the array must match with object keys to work.数组中对象的键必须与对象键匹配才能工作。
I always use it when I need to filter an array based on various conditions.当我需要根据各种条件过滤数组时,我总是使用它。
export const filterArray = (filterData, toBeFiltered) => {
return toBeFiltered.filter(item => {
for (const key in filterData) {
if (item[key] === null || item[key] === undefined) {
return false;
} else {
if (typeof filterData[key] === 'string') {
if (!(item[key]).toLowerCase().includes((filterData[key].trim()).toLowerCase())) {
return false;
}
} else if (item[key] !== filterData[key]) {
return false;
}
}
}
return true;
});
};
Hope it helps.希望能帮助到你。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.