Фильтруются только строки (name, description), числовые значения нет (price, quantity). Почему?
class Product {
constructor(name, price, quantity, description) {
this.name = name;
this.price = price;
this.quantity = quantity;
this.description = description;
}
}
const products = [
new Product("Product 1", 10, 5, "This is product 1a"),
new Product("Product 2", 20, 6, "This is product 2c"),
new Product("Product 3", 30, 8, "This is product 3"),
new Product("Product 4", 40, 2, "cThis is product 4"),
];
function filterProducts(query) {
const parts = query.split("&");
return products.filter((product) => {
return parts.every((part) => {
const [field, operator, value] = part.split("-");
switch (operator) {
case "contains":
return product[field].includes(value);
case "starts":
return product[field].startsWith(value);
case "ends":
return product[field].endsWith(value);
case "<":
return product[field] < Number(value);
case "=":
return product[field] === Number(value);
case ">":
return product[field] > Number(value);
case "<=":
return product[field] <= Number(value);
case ">=":
return product[field] >= Number(value);
default:
return false;
}
});
});
}
console.log(filterProducts("name-contains-4&description-starts-c")); // 'Product 4' -- верно
console.log(filterProducts("price->=20&quantity-=6")); // [] -- неверно. почему?