【问题标题】:How to filter array of JSON-objects with multiple integer conditions and key values如何过滤具有多个整数条件和键值的 JSON 对象数组
【发布时间】:2020-08-13 13:56:40
【问题描述】:

我在 VueJS (It's a app with drop downs and ranges @ codepen) 中使用交互式搜索过滤器时遇到了困难

一艘船有 BrandName、BrandYear、Price...,我可以使用 selected = {...} 过滤这些信息,但我想知道如何充分利用下面的 if-statement ,通过expected_selected = {...}来识别价格并检查最小值/最大值并返回结果

我正在寻找有关如何过滤最小/最大值以及以下代码的解释/帮助。

目标是输入最小值和最大值以及一个或多个匹配的键值

var boats = [{
  Price: 599900,
  BrandName: "FLIPPER",
  BoatYear: 2020,
}, {
  Price: 97e3,
  BrandName: "MICORE",
  BoatYear: 2020,
}, {
  Price: 189300,
  BrandName: "LINDER",
  BoatYear: 2020,
}, {
  Price: 396900,
  BrandName: null,
  BoatYear: 2020,
}, {
  Price: 334900,
  BrandName: "MICORE",
  BoatYear: 2019,
}, {
  Price: 138700,
  BrandName: "HR",
  BoatYear: 2020,
}, {
  Price: 178900,
  BrandName: "HR",
  BoatYear: 2020,
}, {
  Price: 348900,
  BrandName: "HR",
  BoatYear: 2020,
}, {
  Price: 285800,
  BrandName: "HR",
  BoatYear: 2020,
}, {
  Price: 186900,
  BrandName: "MICORE",
  BoatYear: 2019,
}, {
  Price: 276800,
  BrandName: "MICORE",
  BoatYear: 2020,
}, {
  Price: 518900,
  BrandName: "SILVER",
  BoatYear: 2020,
}, {
  Price: 226900,
  BrandName: "MICORE",
  BoatYear: 2020,
}, {
  Price: 132600,
  BrandName: "LINDER",
  BoatYear: 2020,
}, {
  Price: 137200,
  BrandName: "LINDER",
  BoatYear: 2020,
}, {
  Price: 366900,
  BrandName: "SILVER",
  BoatYear: 2020,
}, {
  Price: 365900,
  BrandName: "SILVER",
  BoatYear: 2020,
}, {
  Price: 247900,
  BrandName: "SILVER",
  BoatYear: 2020,
}];


var selected = {
  BoatYear: 2020,
  BrandName: "LINDER"
};

var expected_selected = {
  BoatYear: 2020,
  BrandName: 'LINDER',
  Price: [0, 138000] // min , max 
}

boats = boats.filter(function(item) {
  for (var key in selected) {
    if (item[key] === undefined || item[key] != selected[key]) return false;
  }
  return true;
});

console.log(`Results: ${JSON.stringify(boats)}`);

【问题讨论】:

  • 你希望从你的 sn-p 得到什么输出?
  • @Nikolas,想要的输出是一个对象数组,目前在 Vue-app 中它是一个 computed value

标签: json vue.js filter conditional-statements filtering


【解决方案1】:
  1. 最简单的解决方案:只需硬编码所有字段

let boats = [
  {Price: 599900, BrandName: "FLIPPER", BoatYear: 2020},
  {Price: 97e3  , BrandName: "MICORE" , BoatYear: 2020},
  {Price: 189300, BrandName: "LINDER" , BoatYear: 2020},
  {Price: 396900, BrandName: null     , BoatYear: 2020},
  {Price: 334900, BrandName: "MICORE" , BoatYear: 2019},
  {Price: 138700, BrandName: "HR"     , BoatYear: 2020},
  {Price: 178900, BrandName: "HR"     , BoatYear: 2020},
  {Price: 348900, BrandName: "HR"     , BoatYear: 2020},
  {Price: 285800, BrandName: "HR"     , BoatYear: 2020},
  {Price: 186900, BrandName: "MICORE" , BoatYear: 2019},
  {Price: 276800, BrandName: "MICORE" , BoatYear: 2020},
  {Price: 518900, BrandName: "SILVER" , BoatYear: 2020},
  {Price: 226900, BrandName: "MICORE" , BoatYear: 2020},
  {Price: 132600, BrandName: "LINDER" , BoatYear: 2020},
  {Price: 137200, BrandName: "LINDER" , BoatYear: 2020},
  {Price: 366900, BrandName: "SILVER" , BoatYear: 2020},
  {Price: 365900, BrandName: "SILVER" , BoatYear: 2020},
  {Price: 247900, BrandName: "SILVER" , BoatYear: 2020}
];

const expected_selected = {
  BoatYear : 2020,
  BrandName: 'LINDER',
  Price    : { min: 0, max: 138000 },
}
const filter_by = filters => item => {
  if (item.BoatYear === undefined || item.BoatYear !== filters.BoatYear ) return false
  if (item.BrandName === undefined || item.BrandName !== filters.BrandName ) return false
  if (item.Price < filters.Price.min || item.Price > filters.Price.max) return false
  return true
}
boats = boats.filter(filter_by(expected_selected))

console.log(`Results: ${JSON.stringify(boats)}`);
  1. 或者在任何地方使用最小/最大

let boats = [
  {Price: 599900, BrandName: "FLIPPER", BoatYear: 2020},
  {Price: 97e3  , BrandName: "MICORE" , BoatYear: 2020},
  {Price: 189300, BrandName: "LINDER" , BoatYear: 2020},
  {Price: 396900, BrandName: null     , BoatYear: 2020},
  {Price: 334900, BrandName: "MICORE" , BoatYear: 2019},
  {Price: 138700, BrandName: "HR"     , BoatYear: 2020},
  {Price: 178900, BrandName: "HR"     , BoatYear: 2020},
  {Price: 348900, BrandName: "HR"     , BoatYear: 2020},
  {Price: 285800, BrandName: "HR"     , BoatYear: 2020},
  {Price: 186900, BrandName: "MICORE" , BoatYear: 2019},
  {Price: 276800, BrandName: "MICORE" , BoatYear: 2020},
  {Price: 518900, BrandName: "SILVER" , BoatYear: 2020},
  {Price: 226900, BrandName: "MICORE" , BoatYear: 2020},
  {Price: 132600, BrandName: "LINDER" , BoatYear: 2020},
  {Price: 137200, BrandName: "LINDER" , BoatYear: 2020},
  {Price: 366900, BrandName: "SILVER" , BoatYear: 2020},
  {Price: 365900, BrandName: "SILVER" , BoatYear: 2020},
  {Price: 247900, BrandName: "SILVER" , BoatYear: 2020},
]

const expected_selected = {
  BoatYear : { min: 2020    , max: 2020     },
  BrandName: { min: 'LINDER', max: 'LINDER' },
  Price    : { min: 0       , max: 138000   },
}
const filter_by = filters => item => {
  for (var key in filters) {
    if (item[key] === undefined) return false
    if (item[key] < filters[key].min || item[key] > filters[key].max) return false
  }
  return true
}
boats = boats.filter(filter_by(expected_selected))

console.log(`Results: ${JSON.stringify(boats)}`);
  1. 或者检查selected字段的类型(在这种情况下Array.isArray,在{min,max}的情况下它将是instanceof

let boats = [
  {Price: 599900, BrandName: "FLIPPER", BoatYear: 2020},
  {Price: 97e3  , BrandName: "MICORE" , BoatYear: 2020},
  {Price: 189300, BrandName: "LINDER" , BoatYear: 2020},
  {Price: 396900, BrandName: null     , BoatYear: 2020},
  {Price: 334900, BrandName: "MICORE" , BoatYear: 2019},
  {Price: 138700, BrandName: "HR"     , BoatYear: 2020},
  {Price: 178900, BrandName: "HR"     , BoatYear: 2020},
  {Price: 348900, BrandName: "HR"     , BoatYear: 2020},
  {Price: 285800, BrandName: "HR"     , BoatYear: 2020},
  {Price: 186900, BrandName: "MICORE" , BoatYear: 2019},
  {Price: 276800, BrandName: "MICORE" , BoatYear: 2020},
  {Price: 518900, BrandName: "SILVER" , BoatYear: 2020},
  {Price: 226900, BrandName: "MICORE" , BoatYear: 2020},
  {Price: 132600, BrandName: "LINDER" , BoatYear: 2020},
  {Price: 137200, BrandName: "LINDER" , BoatYear: 2020},
  {Price: 366900, BrandName: "SILVER" , BoatYear: 2020},
  {Price: 365900, BrandName: "SILVER" , BoatYear: 2020},
  {Price: 247900, BrandName: "SILVER" , BoatYear: 2020},
]

const expected_selected = {
  BoatYear : 2020,
  BrandName: 'LINDER',
  Price    : [ 0, 138000 ],
}
const filter_by = filters => item => {
  for (var key in filters) {
    if (item[key] === undefined) return false
    if (Array.isArray(filters[key])) {
      if(item[key] < filters[key][0] || item[key] > filters[key][1]) return false
    } else if (item[key] !== filters[key]) return false
  }
  return true
}
boats = boats.filter(filter_by(expected_selected))

console.log(`Results: ${JSON.stringify(boats)}`);
  1. 或者更好的是,使用 OOP

let boats = [
  {Price: 599900, BrandName: "FLIPPER", BoatYear: 2020},
  {Price: 97e3  , BrandName: "MICORE" , BoatYear: 2020},
  {Price: 189300, BrandName: "LINDER" , BoatYear: 2020},
  {Price: 396900, BrandName: null     , BoatYear: 2020},
  {Price: 334900, BrandName: "MICORE" , BoatYear: 2019},
  {Price: 138700, BrandName: "HR"     , BoatYear: 2020},
  {Price: 178900, BrandName: "HR"     , BoatYear: 2020},
  {Price: 348900, BrandName: "HR"     , BoatYear: 2020},
  {Price: 285800, BrandName: "HR"     , BoatYear: 2020},
  {Price: 186900, BrandName: "MICORE" , BoatYear: 2019},
  {Price: 276800, BrandName: "MICORE" , BoatYear: 2020},
  {Price: 518900, BrandName: "SILVER" , BoatYear: 2020},
  {Price: 226900, BrandName: "MICORE" , BoatYear: 2020},
  {Price: 132600, BrandName: "LINDER" , BoatYear: 2020},
  {Price: 137200, BrandName: "LINDER" , BoatYear: 2020},
  {Price: 366900, BrandName: "SILVER" , BoatYear: 2020},
  {Price: 365900, BrandName: "SILVER" , BoatYear: 2020},
  {Price: 247900, BrandName: "SILVER" , BoatYear: 2020},
]

class MinMax {
  constructor(min, max) { this.min = min, this.max = max }
  check(val) { return val >= this.min && val <= this.max }
}
class Eq {
  constructor(val) { this.val = val }
  check(val) { return val === this.val }
}
var expected_selected = {
  BoatYear : new Eq(2020),
  BrandName: new Eq('LINDER'),
  Price    : new MinMax(0, 138000)
}
const filter_by = filters => item => {
  for (var key in filters) {
    if (item[key] === undefined) return false
    if (filters[key].check(item[key]) === false) return false
  }
  return true
}
boats = boats.filter(filter_by(expected_selected))

console.log(`Results: ${JSON.stringify(boats)}`);

这样,您可以通过添加新类来扩展您的过滤器,而无需更改 filter_by 函数。

【讨论】:

    【解决方案2】:

    当我们有许多属性要验证时,使用键数组检查对象是有意义的。在您的情况下,您有 4 个需要通过相等性检查的属性和 3 个需要通过范围检查的属性,因此,如果您想"Don't Repeat Yourself",您可以使用您的示例和您的项目。

    您可以为要检查的每种类型创建一个键数组,然后在一个过滤器中通过所有键验证所有这些条件。对于此示例,您将拥有一个包含需要通过相等性检查的值的键的数组和一个包含需要通过整数最小/最大范围检查的值的键的数组:

    let boats = [
      {Price:599900, BrandName:"FLIPPER", BoatYear:2020},
      {Price:97000, BrandName:"MICORE", BoatYear:2020},
      {Price:189300, BrandName:"LINDER", BoatYear:2020},
      {Price:396900, BrandName:null, BoatYear:2020},
      {Price:334900, BrandName:"MICORE", BoatYear:2019},
      {Price:138700, BrandName:"HR", BoatYear:2020},
      {Price:178900, BrandName:"HR", BoatYear:2020},
      {Price:348900, BrandName:"HR", BoatYear:2020},
      {Price:285800, BrandName:"HR", BoatYear:2020},
      {Price:186900, BrandName:"MICORE", BoatYear:2019},
      {Price:276800, BrandName:"MICORE", BoatYear:2020},
      {Price:518900, BrandName:"SILVER", BoatYear:2020},
      {Price:226900, BrandName:"MICORE", BoatYear:2020},
      {Price:132600, BrandName:"LINDER", BoatYear:2020},
      {Price:137200, BrandName:"LINDER", BoatYear:2020},
      {Price:366900, BrandName:"SILVER", BoatYear:2020},
      {Price:365900, BrandName:"SILVER", BoatYear:2020},
      {Price:247900, BrandName:"SILVER", BoatYear:2020}
    ];
    
    let expected = {
      BoatYear: 2020,
      BrandName: 'LINDER',
      Price: [0, 138000] // min, max 
    }
    
    // Keys that need to be checked by equality
    const equals = ['BrandName', 'BoatYear', /* 'MotoBoatType', 'EngineModel' */];
    
    // Keys that need to be checked by range
    const ranges = ['Price', /* 'Width', 'Length' */]
    
    boats = boats.filter((item) => {
      // First check the equality keys
      for (const field of equals)
        if (expected[field] && item[field] !== expected[field]) return false;
      
      // Then check the range keys
      for (const field of ranges)
        if (item[field] < expected[field][0] || item[field] > expected[field][1]) return false;
    
      return true;
    });
    
    console.log(`Results: ${boats.length}`, 
      boats.map(({ Price, BrandName, BoatYear }) => `${BrandName} (${BoatYear}) : ${Price}`)
    );

    您甚至可以使用 Array.prototype.every() 将过滤器代码设置为 2 行来验证数组键:

    let boats = [
      {Price:599900, BrandName:"FLIPPER", BoatYear:2020},
      {Price:97000, BrandName:"MICORE", BoatYear:2020},
      {Price:189300, BrandName:"LINDER", BoatYear:2020},
      {Price:396900, BrandName:null, BoatYear:2020},
      {Price:334900, BrandName:"MICORE", BoatYear:2019},
      {Price:138700, BrandName:"HR", BoatYear:2020},
      {Price:178900, BrandName:"HR", BoatYear:2020},
      {Price:348900, BrandName:"HR", BoatYear:2020},
      {Price:285800, BrandName:"HR", BoatYear:2020},
      {Price:186900, BrandName:"MICORE", BoatYear:2019},
      {Price:276800, BrandName:"MICORE", BoatYear:2020},
      {Price:518900, BrandName:"SILVER", BoatYear:2020},
      {Price:226900, BrandName:"MICORE", BoatYear:2020},
      {Price:132600, BrandName:"LINDER", BoatYear:2020},
      {Price:137200, BrandName:"LINDER", BoatYear:2020},
      {Price:366900, BrandName:"SILVER", BoatYear:2020},
      {Price:365900, BrandName:"SILVER", BoatYear:2020},
      {Price:247900, BrandName:"SILVER", BoatYear:2020}
    ];
    
    let expected = {
      BoatYear: 2020,
      BrandName: 'LINDER',
      Price: [0, 138000] // min, max 
    }
    
    const equals = ['BrandName', 'BoatYear', /* 'MotoBoatType', 'EngineModel' */];
    const ranges = ['Price', /* 'Width', 'Length' */]
    
    boats = boats.filter((item) => 
      equals.every(field => !expected[field] || item[field] === expected[field]) &&
      ranges.every(field => item[field] >= expected[field][0] && item[field] <= expected[field][1])
    );
    
    console.log(`Results: ${boats.length}`, 
      boats.map(({ Price, BrandName, BoatYear }) => `${BrandName} (${BoatYear}) : ${Price}`)
    );

    你可以在fork 上查看我在Codepen 上的演示项目。它具有相同的方法,并将验证应用于所有范围键 PriceWidthLength

    【讨论】:

      【解决方案3】:

      您可能已经找到了解决方案,但只是为了把我的想法放在这里,我发布了这个答案。我创建了一个通用函数,它将采用一个对象(过滤器对象),该对象可以具有原始值、对象或数组。希望这会有所帮助

      let boats = [
        {Price: 599900, BrandName: "FLIPPER", BoatYear: 2020},
        {Price: 97e3  , BrandName: "MICORE" , BoatYear: 2020},
        {Price: 189300, BrandName: "LINDER" , BoatYear: 2020},
        {Price: 396900, BrandName: null     , BoatYear: 2020},
        {Price: 334900, BrandName: "MICORE" , BoatYear: 2019},
        {Price: 138700, BrandName: "HR"     , BoatYear: 2020},
        {Price: 178900, BrandName: "HR"     , BoatYear: 2020},
        {Price: 348900, BrandName: "HR"     , BoatYear: 2020},
        {Price: 285800, BrandName: "HR"     , BoatYear: 2020},
        {Price: 186900, BrandName: "MICORE" , BoatYear: 2019},
        {Price: 276800, BrandName: "MICORE" , BoatYear: 2020},
        {Price: 518900, BrandName: "SILVER" , BoatYear: 2020},
        {Price: 226900, BrandName: "MICORE" , BoatYear: 2020},
        {Price: 132600, BrandName: "LINDER" , BoatYear: 2020},
        {Price: 137200, BrandName: "LINDER" , BoatYear: 2020},
        {Price: 366900, BrandName: "SILVER" , BoatYear: 2020},
        {Price: 365900, BrandName: "SILVER" , BoatYear: 2020},
        {Price: 247900, BrandName: "SILVER" , BoatYear: 2020}
      ];
      
      var expected_selected = {
        BoatYear: 2020,
        BrandName: 'LINDER',
        Price: [0, 138000] // min , max 
      }
      
      var expected_selected_2 = {
        BoatYear: 2020,
        BrandName: 'LINDER',
        Price: {min:0, max:138000} 
      }
      
      var expected_selected_3 = {
        BoatYear: 2020,
        BrandName: 'LINDER',
        Price: 137200
      }
      
      const filter_by = (filter) => item => {
      for(let key in filter) {
        if(filter.hasOwnProperty(key)){
            if(Array.isArray(filter[key])) {
              if(!item[key] || item[key] < filter[key][0] || item[key] > filter[key][1]) return false;
            }
            else if(typeof filter[key] === "object") {
              if(!item[key] || item[key] < filter[key]["min"] || item[key] > filter[key]["max"]) return false;
            }
            else {
              if(!item[key] || item[key] !== filter[key]) return false;
            }
          } else {
            return false
          }
        }
        return true
      }
      
      const results = boats.filter(filter_by(expected_selected))
      console.log(`Results: ${JSON.stringify(results, null, 2)}`);
      
      const results_2 = boats.filter(filter_by(expected_selected_2))
      console.log(`Results_2: ${JSON.stringify(results_2, null, 2)}`);
      
      const results_3 = boats.filter(filter_by(expected_selected_3))
      console.log(`Results_3: ${JSON.stringify(results_3, null, 2)}`);

      【讨论】:

        【解决方案4】:

        您可以为每个字段设置一个“匹配器”方法,例如:

        // ...
        
        var selected = {
          BoatYear: 2020,
          BrandName: "LINDER"
        };
        
        var expected_selected = {
          BoatYear: 2020,
          BrandName: 'LINDER',
          Price: [0, 138000] // min , max 
        }
        
        var matchers = {
          BoatYear: (value, input) => value == input,
          BrandName: (value, input) => value == input,
          Price: (value, [min, max]) => value >= min && value <= max,
        };
        
        boats = boats.filter(function(item) {
          for (var key in selected) {
            if (item[key] === undefined || matchers[key](item[key], selected[key])) return false;
          }
          return true;
        });
        

        这样很容易添加不同类型的过滤器,你甚至可以声明一次,如果它们相同就可以重复使用:

        var eqMatcher = (value, input) => value == input;
        var rangeMatcher = (value, [min, max]) => value >= min && value <= max;
        
        var matchers = {
          BoatYear: eqMatcher,
          BrandName: eqMatcher,
          Price: rangeMatcher,
        };
        

        【讨论】:

          猜你喜欢
          • 2021-07-05
          • 2023-01-10
          • 1970-01-01
          • 2021-12-10
          • 1970-01-01
          • 2017-09-21
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多