如果另一个对象中的多个条件为true,则过滤一个对象
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);
我有一系列对象,我试图用另一个对象动态过滤。我很难弄清逻辑,以通过过滤器对象中的所有条件过滤我的数据。
FilterData当前正在返回具有IDS 2,3,4,5和6的对象。 我希望它仅返回3,4,5,因为这些行符合我的过滤器数组中的两个条件。 显然,返回2,3,4,5和6,因为我的逻辑仅需要数组中的一个条件才是真实的,但是我不确定该怎么做。
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);
I have an array of objects I am trying to dynamically filter with another array of objects. I am having trouble figuring out the logic for filtering my data by all the conditions in my filter object.
filteredData is currently returning the objects with the ids 2,3,4,5 and 6.
I want it to return only 3,4,5 since those rows meet BOTH conditions in my filter array.
Obviously, 2,3,4,5 and 6 are being returned because my logic is only requiring one condition in the array to be true, but I am not sure what to do about that.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以 每个 过滤条件保持真实。
You can
filter
those data objects whereevery
filter condition holds true.您可以迭代过滤器并检查所有约束是否为真。
You could iterate the filters and check if all constraints are true.
您可以使用函数
array.prototype.ey
检查整个条件是否为真。You can use the function
Array.prototype.every
to check that the whole set of conditions is true.