我为选项“filterProductBy(options)”写了一个排序方法,告诉我如何使这个方法通用,不管参数有多少,我都可以应用这个过滤器。请告诉我该怎么做?
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);
}
//method for filtering products by specified parameters
filterProductBy(options) {
const optionName = options.name,
optionCount = options.count,
optionPrice = options.price;
const filters = {
byName: optionName == undefined ? () => undefined :
(actualName, optionName) => actualName === optionName,
byCount: optionCount == undefined ? () => undefined :
new Function("actualName, optionName", "return actualName " + optionCount),
byPrice: optionPrice == undefined ? () => undefined :
new Function("actualName, optionName", "return actualName " + optionPrice)
};
return this.products.filter(
(product) => filters.byName(product.name, optionName)
|| filters.byCount(product.count, optionCount)
|| filters.byPrice(product.price, optionPrice));
}
}
const shop = new Shop();
shop.addProduct(new Product("product 1", 1, 2000));
shop.addProduct(new Product("item 2", 2, 100));
shop.addProduct(new Product("some 3", 30, 300));
console.log(shop.filterProductBy({
name: "product 1",
count: ">=4",
price: ">500"
}));
最佳答案
它几乎不需要那么复杂。只需遍历您获得的对象的属性,并包含一个项目(如果它与其中任何一个匹配)(因为您的过滤器是“OR”过滤器):
filterProductBy(options) {
var keys = Object.keys(options);
return this.products.filter(product => keys.some(key => product[key] == options[key]));
}
如果 ==
不适用于所有选项,您可以在 key
上使用 switch
(在 一些
回调)来选择正确的关系来使用。
实例:
var products = [
{name: "Widget", count: 12, price: 100},
{name: "Gadget", count: 2, price: 70},
{name: "Thingy", count: 14, price: 80}
];
function filterProductBy(options) {
var keys = Object.keys(options);
return this.products.filter(product => keys.some(key => product[key] == options[key]));
}
console.log("name: Widget", filterProductBy({name: "Widget"}));
console.log("name: Widget, count: 2", filterProductBy({name: "Widget", count: 2}));
获取 options
的键作为一个数组,然后在 filter
回调中使用这些键,通过使用 some
查看产品是否与它们中的任何一个匹配.
如果您想要一个“AND”过滤器,您可以使用 every
而不是一些
:
filterProductBy(options) {
var keys = Object.keys(options);
return this.products.filter(product => keys.every(key => product[key] == options[key]));
}
实例:
var products = [
{name: "Widget", count: 12, price: 100},
{name: "Gadget", count: 2, price: 70},
{name: "Thingy", count: 14, price: 80}
];
function filterProductBy(options) {
var keys = Object.keys(options);
return this.products.filter(product => keys.every(key => product[key] == options[key]));
}
console.log("name: Widget", filterProductBy({name: "Widget"}));
console.log("name: Widget, count: 14", filterProductBy({name: "Widget", count: 14}));
console.log("name: Thingy, count: 14", filterProductBy({name: "Thingy", count: 14}));
关于javascript - 如何使按选项排序的方法具有通用性?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51249096/