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

发布于 2025-02-11 11:39:04 字数 1910 浏览 2 评论 0原文

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

故人爱我别走 2025-02-18 11:39:04

您可以 每个 过滤条件保持真实。

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

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

陪你搞怪i 2025-02-18 11:39:04

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

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

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

勿忘心安 2025-02-18 11:39:04

您可以使用函数array.prototype.ey检查整个条件是否为真。

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

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文