[英]JavaScript: how to iteratively filter an array?
I want to be able to filter an array with one or many conditions.我希望能够过滤具有一个或多个条件的数组。
Consider this sample data & filter criteria:考虑此示例数据和过滤条件:
var data = [
{company: "Acme", fiscal_period: '01-2019', value: 10},
{company: "Acme", fiscal_period: '02-2019', value: 15},
{company: "Wonka", fiscal_period: '01-2019', value: 8},
{company: "Wonka", fiscal_period: '02-2019', value: 11}
]
var filter = [{field: "company", value: "Acme"}]
console.log(myFilterFunction(data,filter));
// Desired output
// [
// {company: "Acme", fiscal_period: '01-2019', value: 10},
// {company: "Acme", fiscal_period: '02-2019', value: 15}
// ]
// Now use two filters:
var filter = [{field: "company", value: "Acme"},{field: "fiscal_period", value: "01-2019"}]
console.log(myFilterFunction(data,filter);
// Desired output
// [
// {company: "Acme", fiscal_period: '01-2019', value: 10},
// ]
But how to write the myFilterFunction?但是如何编写 myFilterFunction 呢?
I know how to make it work with a static filter:我知道如何让它与静态过滤器一起工作:
myFilterFunction = function(data,filter){
return data.filter(function(el){
return el[filter.field] === filter.value;
});
}
console.log(myFilterFunction(data,{field: "company", value: "Acme"});
But how to make it work dynamically if I want to have multiple filters?但是如果我想有多个过滤器,如何让它动态工作?
Pass multiple field data as an array and use Array#every
method to check all filters which return true
only if all return values are true
.传递多个字段的数据作为阵列和使用
Array#every
方法来检查哪个返回所有滤波器true
只有当所有的返回值是true
。
myFilterFunction = function(data,filters){
return data.filter(function(el){
return filters.every(filter => el[filter.field] === filter.value);
});
}
console.log(myFilterFunction(data,[{field: "company", value: "Acme"},{field: "value", value: 10}]));
var data = [{ company: "Acme", fiscal_period: '01-2019', value: 10 }, { company: "Acme", fiscal_period: '02-2019', value: 15 }, { company: "Wonka", fiscal_period: '01-2019', value: 8 }, { company: "Wonka", fiscal_period: '01-2019', value: 11 } ] myFilterFunction = function(data, filters) { return data.filter(function(el) { return filters.every(filter => el[filter.field] === filter.value); }); } console.log(myFilterFunction(data, [{ field: "company", value: "Acme" }, { field: "value", value: 10 }]));
myFilterFunction = (data,filters) => {
return data.filter(el => filters.every(({ field, value }) => el[field] === value));
}
var data = [{ company: "Acme", fiscal_period: '01-2019', value: 10 }, { company: "Acme", fiscal_period: '02-2019', value: 15 }, { company: "Wonka", fiscal_period: '01-2019', value: 8 }, { company: "Wonka", fiscal_period: '01-2019', value: 11 } ] myFilterFunction = (data,filters) => { return data.filter(el => filters.every(({ field, value }) => el[field] === value)); } console.log(myFilterFunction(data, [{ field: "company", value: "Acme" }, { field: "value", value: 10 }]));
data.filter(el=>{
for(let t in filterArray){
if(el[t]!=filterArray[t]){
return false;
}
}
return true;
})
like this像这样
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.