如何实施使用指定选项过滤商品的方法?

时间:2018-06-07 13:39:58

标签: javascript ecmascript-6

例如,该方法将一个options对象作为参数,该对象包含用于搜索{name:“item 2”,price:“< = 1000”,count:“> = 2”}每个选项的参数是可选的。该方法必须返回带有货物的过滤数组。 filter By(options)。必需时,必须通过解构接收此对象的参数。

我用这种方式实现了这个方法,但是错了:

    filterProductBy(dataset, filters, options) {
        this.options = options;
        let filtredFirst = dataset.map(x => {
            for (let subObjectName in x) {
                let inner = x[subObjectName];
                return inner;
            }
        });
        let filtred = [];
        for (let prop in filtres) {
            console.log(prop);
            let filtredInst = filtredFirst.filter(x => filters[prop](x[prop]));
            console.log(filtredInst);
            filtred = filtred.concat(filtredInst);
        }
        return filtred;
    }
console.log(shop.filterProductBy({
    name: "product",
    count: ">=10",
    price: ">=200"
}));

告诉我如何正确实施

我的所有代码

//Product Creation Class
class Product {
    constructor(name, count, price) {
        this.name = name;
        this.count = count;
        this.price = price;
    }
}
//Сlass where products are recorded
class Shop {
    constructor(products) {
        this.products = [];
    }

    //method for adding a product
    addProduct(newProduct) {
        this.products.push(newProduct);
    }
    filterProductBy(dataset, filters, options) {
        this.options = options;
        let filtredFirst = dataset.map(x => {
            for (let subObjectName in x) {
                let inner = x[subObjectName];
                return inner;
            }
        });
        let filtred = [];
        for (let prop in filtres) {
            console.log(prop);
            let filtredInst = filtredFirst.filter(x => filters[prop](x[prop]));
            console.log(filtredInst);
            filtred = filtred.concat(filtredInst);
        }
        return filtred;
    }
}

const shop = new Shop();
// create products
shop.addProduct(new Product("product 1", 1, 2000));
shop.addProduct(new Product("product 2", 1, 700));
shop.addProduct(new Product("product 3", 2, 800));
shop.addProduct(new Product("product 4", 3, 1000));
console.log(shop.filterProductBy({
    name: "product",
    count: ">=10",
    price: ">=200"
}));

1 个答案:

答案 0 :(得分:1)

根据你的问题,这是我能得到的最接近的问题。我猜测filterProductBy采取选项,并且它应该过滤商店所拥有的产品。

它似乎可以满足您的要求,但目前仅适用于滤镜> =和< =



const getEvaluationFunction = filterString => filterString.indexOf('>=') > -1 ?
  (number, amount) => number >= amount :
  (number, amount) => number <= amount;


        //Product Creation Class
        class Product {
            constructor(name, count, price) {
                this.name = name;
                this.count = count;
                this.price = price;
            }
        }
        //Сlass where products are recorded
        class Shop {
            constructor(products) {
                this.products = [];
            }

            //method for adding a product
            addProduct(newProduct) {
                this.products.push(newProduct);
            }
            filterProductBy({name, price, count}) {
              return this.products
                .filter(product => name === undefined || product.name === name)
                .filter(product => price === undefined ||
                    getEvaluationFunction(price)(product.price, price.match(/(\d+)/)[0])
                 )
                 .filter(product => count === undefined ||
                    getEvaluationFunction(count)(product.count, count.match(/(\d+)/)[0])
                 )
            }
        }

        const shop = new Shop();
        // create products
        shop.addProduct(new Product("product 1", 1, 2000));
        shop.addProduct(new Product("product 2", 1, 700));
        shop.addProduct(new Product("product 3", 2, 800));
        shop.addProduct(new Product("product 4", 3, 1000));
        console.log(shop.filterProductBy({
            price: ">=1000",
            count: ">=2",
        }));
&#13;
&#13;
&#13;