Home > Back-end >  Filter one object if multiple conditions in another object are true
Filter one object if multiple conditions in another object are true

Time:06-29

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.

CodePudding user response:

You could iterate the filters and check if all constraints are true.

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; }

CodePudding user response:

You can use the function Array.prototype.every to check that the whole set of conditions is true.

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; }

CodePudding user response:

You can filter those data objects where every filter condition holds true.

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);

  • Related