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.
filtermethod takes a function that returns a Boolean value; true to include the value, false to exclude. So your filter call could just beitemKeys.filter(function(key) { return key === filterType; })oritemKeys.filter((key) => key === filterType)if you want to use JavaScript valid since 2015.