我有三种输入价格,分别来自范围输入,名称来自文本输入和select中的类别。T尝试使用多个if语句并使用三个单独的函数作为参数。哪种更好,并且可读性和优雅性更高这样做
//I will put the variables and the html in case you want to see them
const elements = e.target.elements;
const name = elements.name.value.trim().toLowerCase();
const price = parseInt(elements.price.value);
const category = elements.categories.value;
const filterPriceMaxValue = parseInt(document.querySelector(".filter-price").max);
const filterAll = (products, category, name, price, filterPriceMaxValue) => {
if (name && price < filterPriceMaxValue && category === '') {
return products.filter(
product => product.name === name && product.price <= price
);
} else if (name !== '' && price < filterPriceMaxValue) {
return products.filter(
product =>
product.name === name &&
product.price <= price &&
product.category === category
);
} else if (name === '' && price < filterPriceMaxValue && category === '') {
return products.filter(product => product.price <= price);
} else if (name === '' && price === filterPriceMaxValue && category !== '') {
return products.filter(product => product.category === category);
} else if (name !== '' && price === filterPriceMaxValue) {
return products.filter(product => product.name === name);
} else if (name === '' && price < filterPriceMaxValue && category !== '') {
return products.filter(
product => product.price <= price && product.category === category
);
}
return products;
};
//Or i tried using separate functions as arguments**strong text**
filterCategory(filterName(filterPrice(products, price, filterPriceMaxValue), name), category)
const filterName = (products, name) => {
if (name !== '') {
return products.filter(product => product.name === name)
}
return products
}
const filterPrice = (products, price, filterPriceMaxValue) => {
if (price < filterPriceMaxValue) {
return products.filter(product => product.price <= price);
}
return products;
};
const filterCategory = (products, category) => {
if (category !== '') {
return products.filter(product => product.category === category);
}
return products;
};
<form class="filter-form">
<select name="categories" class="filter-categories">
<option disabled selected value> -- select a category -- </option>
<option value="sport">sport</option>
<option value="accessories">accessories</option>
<option value="shoes">shoes</option>
<option value="clothes">clothes</option>
</select>
<input type="text" class="filter-name" name="name" placeholder="product">
<input type="range" value="1000" min="0" max="1000" name="price" class="filter-price">
<label for="price" class="price-label">Price Range:<span class="price-span"></span></label>
<button class="filter-btn">Search</button>
</form>
答案 0 :(得分:0)
您可以为每个想要的过滤器部件使用一个功能,然后使用此功能检查要过滤的产品。
var elements = e.target.elements,
fName = elements.name.value.trim().toLowerCase(),
fPrice = +elements.price.value,
fCategory = elements.categories.value,
fMaxPrice = +document.querySelector(".filter-price").max,
filters = [],
products = [
{
category: 'sport',
price: 42,
name: 'shorts'
},
// more products
],
filtered;
if (fName) {
filters.push(o => o.name.includes(fName));
}
if (fCategory) {
filters.push(o => o.category === fCategory);
}
if (!isNaN(fPrice)) {
filters.push(o => o.price <= fPrice);
}
if (!isNaN(fMaxPrice)) {
filters.push(o => o.price <= fMaxPrice);
}
filtered = products.filter(p => filters.every(f => f(p)));