如果另一个对象中的多个条件为真,则过滤一个对象



var data = [
{"id": 1, "area": 965, "bedrooms": 1, "floor": 3, "price": 2200},
{"id": 2,"area": 1065, "bedrooms": 2, "floor": 6, "price": 2726},
{"id": 3,"area": 401, "bedrooms": 2, "floor": 4, "price": 2726},
{"id": 4,"area": 699, "bedrooms": 3, "floor": 8, "price": 1232},
{"id": 5,"area": 661, "bedrooms": 1, "floor": 9, "price": 1277},
{"id": 6,"area": 412, "bedrooms": 1, "floor": 12, "price": 518},
{"id": 7,"area": 925, "bedrooms": 2, "floor": 10, "price": 2509},
];
// The filters I want to apply to my raw data
var filters = [
{highValue: 9, lowValue: 4, type: "floor"},
{highValue: 700, lowValue: 400, type: "area"},
];
var filteredData = data.filter(function(item){
for(var key in filters){
//Loop through the filters, get the type and values
var filterType = filters[key].type;
var filterHighValue = filters[key].highValue;
var filterLowValue = filters[key].lowValue;
//Get the object keys from the data.
//Get the particular key that matches the filterType
var itemKeys = Object.keys(item);
var itemKey = itemKeys.filter(function(matchedKey){
if(matchedKey == filterType){
return matchedKey;
}
});

//Eg., if ['floor'] == 'floor'
// Check if the floor value in our data object is between the two values in our filter.
if(filterType == itemKey){
if(item[itemKey] <= filterHighValue && item[itemKey] >= filterLowValue){
return item[itemKey];
}
}
}
});
console.log(filteredData);

我有一个对象数组,我试图用另一个对象数组动态过滤。我在计算过滤对象中所有条件过滤数据的逻辑时遇到了麻烦。

filteredData当前返回id为2、3、4、5和6的对象。我希望它只返回3,4,5,因为这些行满足我的过滤器数组中的两个条件。显然,2、3、4、5和6被返回,因为我的逻辑只要求数组中的一个条件为真,但我不确定该怎么做。

您可以将every过滤器条件为真的数据对象filter

const 
data = [
{ id: 1, area: 965, bedrooms: 1, floor: 3, price: 2200 },
{ id: 2, area: 1065, bedrooms: 2, floor: 6, price: 2726 },
{ id: 3, area: 401, bedrooms: 2, floor: 4, price: 2726 },
{ id: 4, area: 699, bedrooms: 3, floor: 8, price: 1232 },
{ id: 5, area: 661, bedrooms: 1, floor: 9, price: 1277 },
{ id: 6, area: 412, bedrooms: 1, floor: 12, price: 518 },
{ id: 7, area: 925, bedrooms: 2, floor: 10, price: 2509 },
],
filters = [
{ highValue: 9, lowValue: 4, type: "floor" },
{ highValue: 700, lowValue: 400, type: "area" },
],
filteredData = data.filter((d) =>
filters.every((f) => d[f.type] >= f.lowValue && d[f.type] <= f.highValue)
);
console.log(filteredData);

您可以迭代过滤器并检查是否所有约束都为真。

const
data = [{ id: 1, area: 965, bedrooms: 1, floor: 3, price: 2200 }, { id: 2, area: 1065, bedrooms: 2, floor: 6, price: 2726 }, { id: 3, area: 401, bedrooms: 2, floor: 4, price: 2726 }, { id: 4, area: 699, bedrooms: 3, floor: 8, price: 1232 }, { id: 5, area: 661, bedrooms: 1, floor: 9, price: 1277 }, { id: 6, area: 412, bedrooms: 1, floor: 12, price: 518 }, { id: 7, area: 925, bedrooms: 2, floor: 10, price: 2509 }],
filters = [{ highValue: 9, lowValue: 4, type: "floor" }, { highValue: 700, lowValue: 400, type: "area" }],
result = data.filter(o => filters.every(({  highValue, lowValue, type }) =>
o[type] >= lowValue && o[type] <= highValue
));
console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }

您可以使用Array.prototype.every函数来检查整个条件集是否为真。

const data = [{ id: 1, area: 965, bedrooms: 1, floor: 3, price: 2200 }, { id: 2, area: 1065, bedrooms: 2, floor: 6, price: 2726 }, { id: 3, area: 401, bedrooms: 2, floor: 4, price: 2726 }, { id: 4, area: 699, bedrooms: 3, floor: 8, price: 1232 }, { id: 5, area: 661, bedrooms: 1, floor: 9, price: 1277 }, { id: 6, area: 412, bedrooms: 1, floor: 12, price: 518 }, { id: 7, area: 925, bedrooms: 2, floor: 10, price: 2509 }];
const filters = [    {highValue: 9, lowValue: 4, type: "floor"},    {highValue: 700, lowValue:400, type: "area"},];
const result = data.filter(o => {
return filters.every(({highValue, lowValue, type}) => {
return o[type] <= highValue && o[type] >= lowValue; 
});
});
console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }

最新更新