本文介绍了使用可变参数过滤内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

我需要使用getProduct函数过滤我的products数组,该函数接受可变的参数列表.参数可以是产品的名称和/或最低价格最高价格和/或型号.

I need to filter my products array with getProduct function, which accepts changeable list of arguments.Argument can be either the name of the product and/or price within the minPrice, maxPrice, and/or model.

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

推荐答案

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

You can use an object with special structure for searching, if you need t search for more than one item. This proposal uses an object, with this structure for filtering:

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

该算法会查找search中的每个属性,如果 all 比较都为true,则将该元素添加到结果集中.

The algorithm looks for every property in search and if all comparisons are true, then the element is added to the result set.

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' }));
.as-console-wrapper { max-height: 100% !important; top: 0; }

这篇关于使用可变参数过滤内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 18:45