使用可变参数过滤事物

时间:2017-04-07 08:16:47

标签: javascript function object filter arguments

let products = [
{
    "name": "Lenovo",
    "price": "18000",
    "model": "v580c"
},
{
    "name": "Apple",
    "price": "30000",
    "model": "Iphone 6"
},
{
    "name": "Nikon",
    "price": "25000",
    "model": "G290"
}]

我需要使用products函数过滤我的getProduct数组,该函数接受可更改的参数列表。 参数可以是产品的名称和/或 minPrice maxPrice 和/或模型

function getProduct(productName, minPrice, maxPrice, productModel) {

    return products.filter(product => {
        return product.price < maxPrice && product.price > minPrice && product.name == productName;
    });
}

console.log(getProduct("Apple", 3540, 3000000000));
console.log(getProduct("Lenovo", 3540, 3000000000, "v580c"));

2 个答案:

答案 0 :(得分:1)

你可以发送一个参数数组作为参数并写一个逻辑来相应地处理它们。

<强>示例:

&#13;
&#13;
function getProduct(array, params){
  var list = array.filter(function(o){
    return params.every(function(kv){
      if(o.hasOwnProperty(kv.key)){
        var cur = o[kv.key];
        switch (kv.operation){
          case ">":     return cur > kv.value 
          case "<":     return cur < kv.value 
          case "in":    return cur.indexOf(kv.value) > -1
          case "regex": return kv.value.test(cur)
          default:      return cur === kv.value 
        }
      }
    })
  });
  
  console.log(list);
  return list;
}

var products=[{name:"Lenovo",price:"18000",model:"v580c"},{name:"Apple",price:"30000",model:"Iphone 6"},{name:"Nikon",price:"25000",model:"G290"}];

getProduct(products, [{key:"name", value: "Nikon"}])
getProduct(products, [
  {key:"price", value: 20000, operation: ">"},
  {key:"price", value: 40000, operation: "<"}
])

getProduct(products, [{key:"name", value: "e", operation: "in"}])
getProduct(products, [{key:"model", value: /\d{2,}/g, operation: "regex"}])
&#13;
&#13;
&#13;

答案 1 :(得分:1)

如果您需要搜索多个项目,可以使用具有特殊结构的对象进行搜索。该提议使用一个对象,使用此结构进行过滤:

href

该算法在{ name: 'Apple', price: { min: 3540, // both or a single border is possible max: 60000 }, model: function (s) { return s.match(/s/); } // just search for a single letter } 中查找每个属性,如果所有比较都为真,则该元素将添加到结果集中。

&#13;
&#13;
search
&#13;
function filter(array, search) {
    return array.filter(function (a) {
        return Object.keys(search).every(function (k) {
            return (
                a[k] === search[k] ||
                typeof search[k] === 'object' && (
                    ('min' in search[k]) && ('max' in search[k]) && search[k].min <= a[k] && a[k] <= search[k].max ||
                    ('min' in search[k]) !== ('max' in search[k]) && (search[k].min <= a[k] || a[k] <= search[k].max)
                ) ||
                typeof search[k] === 'function' && search[k](a[k])
            );
        });
    });
}

var products = [{ name: "Lenovo", price: "18000", model: "v580c" }, { name: "Apple", price: "30000", model: "Iphone 6" }, { name: "Nikon", price: "25000", model: "G290" }, { name: "Foo", price: "10", model: "a1" }, { name: "Foo", price: "20", model: "a2" }, { name: "Foo", price: "30", model: "a3" }, { name: "Foo", price: "40", model: "a4" }, { name: "Foo", price: "50", model: "a5" }, { name: "Foo", price: "60", model: "a6" }, { name: "Foo", price: "70", model: "a7" }, { name: "Foo", price: "80", model: "a8" }, { name: "Foo", price: "90", model: "a9" }];

console.log(filter(products, { name: 'Foo', price: { min: 60 } }));
console.log(filter(products, { name: 'Foo', price: { max: 40 } }));
console.log(filter(products, { name: 'Foo', price: { min: 40, max: 60 } }));
console.log(filter(products, { name: 'Apple', price: { min: 3540, max: 60000 } }));
console.log(filter(products, { name: 'Lenovo', price: { min: 3540, max: 60000 }, model: 'v580c' }));
&#13;
&#13;
&#13;