Javascript 当结果依赖于多个条件时,防止嵌套if语句
以上是我遇到的一个类似问题的链接。因此,假设我有一个JavaScript对象,其形式如下:Javascript 当结果依赖于多个条件时,防止嵌套if语句,javascript,mongodb,Javascript,Mongodb,以上是我遇到的一个类似问题的链接。因此,假设我有一个JavaScript对象,其形式如下: var query = { minPrice : 30, maxPrice : 50, category : "Clothing" }; 查询对象的属性来自HTML页面的输入元素。如果不使用这个嵌套的if语句结构,我想不出任何其他方法来检查minPrice、maxPrice和category的每个排列。有没有更优雅或性能更好的方法 if (quer
var query = {
minPrice : 30,
maxPrice : 50,
category : "Clothing"
};
查询对象的属性来自HTML页面的输入元素。如果不使用这个嵌套的if语句结构,我想不出任何其他方法来检查minPrice、maxPrice和category的每个排列。有没有更优雅或性能更好的方法
if (queryParams.minPrice) {
if (queryParams.maxPrice) {
if (queryParams.category) {
// minPrice, maxPrice and category
} else {
// minPrice and maxPrice
}
} else if (queryParams.category) {
// minPrice and category
} else {
// minPrice only
console.log('minPrice');
collection.find({price : {$gte: queryParams.minPrice}}).toArray(function(err, docs) {
console.log(docs);
});
}
} else if (queryParams.maxPrice) {
if (queryParams.category) {
// maxPrice and category
} else {
// maxPrice only
}
} else {
// category only
}
提前谢谢
编辑:
function renderMenu(container, storeInstance){
// NOTE: perhaps this is the correct way to empty a container.. Use to
// refactor code..
while (container.lastChild) container.removeChild(container.lastChild);
if (!container._filters) {
// Query object
container._filters = {
minPrice: null,
maxPrice: null,
category: ''
};
container._refresh = function(){
storeInstance.queryProducts(container._filters, function(err, products){
if (err){
alert('Error occurred trying to query products');
console.log(err);
}
else {
displayed = Object.keys(products);
renderProductList(document.getElementById('productView'), storeInstance);
}
});
}
}
var box = document.createElement('div'); container.appendChild(box);
box.id = 'price-filter';
var input = document.createElement('input'); box.appendChild(input);
input.type = 'number';
input.value = container._filters.minPrice;
input.min = 0;
input.placeholder = 'Min Price';
input.addEventListener('blur', function(event){
container._filters.minPrice = event.target.value;
container._refresh();
});
input = document.createElement('input'); box.appendChild(input);
input.type = 'number';
input.value = container._filters.maxPrice;
input.min = 0;
input.placeholder = 'Max Price';
input.addEventListener('blur', function(event){
container._filters.maxPrice = event.target.value;
container._refresh();
});
var list = document.createElement('ul'); container.appendChild(list);
list.id = 'menu';
var listItem = document.createElement('li'); list.appendChild(listItem);
listItem.className = 'menuItem' + (container._filters.category === '' ? ' active': '');
listItem.appendChild(document.createTextNode('All Items'));
listItem.addEventListener('click', function(event){
container._filters.category = '';
container._refresh()
});
var CATEGORIES = [ 'Bedroom', 'Kitchen', 'Office', 'Bathroom' ];
for (var i in CATEGORIES){
var listItem = document.createElement('li'); list.appendChild(listItem);
listItem.className = 'menuItem' + (container._filters.category === CATEGORIES[i] ? ' active': '');
listItem.appendChild(document.createTextNode(CATEGORIES[i]));
listItem.addEventListener('click', (function(i){
return function(event){
container._filters.category = CATEGORIES[i];
container._refresh();
}
})(i));
}
}
使用every()
和filter()
:
var查询={
明普莱斯:30,
最高价格:50,
类别:“服装”
};
//您可以使用:
//Object.values()
//Object.keys()
//Object.entries()
//所有三个都已设置:
if(Object.values(query.every)(e=>e!='null')){
//你的代码
}elseif(Object.values(query).filter(e=>e).length==2){
//已设置maxPrice和类别:
如果(!query.minPrice){}
//已设置最低价格和类别:
如果(!query.maxPrice){}
//设置了minPrice和maxPrice
else{}
}elseif(Object.values(query).filter(e=>e).length==1){
//只设定了最低价格
if(query.minPrice){}
//只设置了maxPrice
else if(query.maxPrice){}
//仅设置类别
else{}
}否则{}
您还没有满足我们对更多代码的要求,因此我将猜测您的查询是如何工作的。请根据需要调整。其思想是,您可以从一个空查询对象开始,并根据指定的参数向其添加属性:
var query = {};
if (queryParams.minPrice || queryParams.maxPrice) {
var price = {};
if (queryParams.minPrice) { price.$gte = queryParams.minPrice; }
if (queryParams.maxPrice) { price.$lte = queryParams.maxPrice; }
query.price = price;
}
if (queryParams.category) { query.category = category; }
collection.find(query).toArray(function(err, docs) {
console.log(docs);
});
这些注释块的内容是什么样的?我看到了唯一的
minPrice
,但是其他的(至少有两个)呢?@CertainPerformance这些注释用于指示查询对象中的属性。例如,var query={minPrice:null,maxPrice:null,category:“countain”};然后你会在那个部分被评论为“仅分类”。是的,很清楚if分支缩小了什么,但是这些块的实际内容是什么?它们不仅仅是空的,是吗?请张贴真实的code@user3100570请告诉我们实际的代码是什么样子的。我们需要知道您在查询中如何实际使用maxPrice
和category
,以便向您展示如何简化此操作。此外,我只能假设您在这里使用的是MongoDB。你应该用它来标记你的问题,而不是HTML或CSS,因为它与这些无关。只有在指定了所有参数时,才会执行任何操作。他们希望根据指定的参数有条件地使用这些参数。Yep-OP要求提供一种避免嵌套if语句的解决方案。这避免了嵌套的if语句(以及长的&&
语句)。它确实避免了if语句,但它并不能远程实现它们试图用代码实现的功能。我想说的是,有一个解决方案在我的代码段中(我对它进行了编辑,以便更好地查看),只是没有完全解决。好的,如果这只是你建议他们做的事情的一部分,那么你就不得不这么说了。FYIObject.values(query).each(e=>e!==“null”)
在这里不起作用,因为'null'
是一个字符串值。我编辑了代码以显示它的使用位置。当查询对象从总是显示在客户机上的输入元素中读取它时,查询对象将始终具有这三个属性,如果有输入,则值将更改。希望这能进一步澄清我的问题。:)@user3100570感谢您添加更多代码,但我们真正需要的是在初始代码中用注释替换的部分。在任何情况下,我认为上述内容都能满足您的需要。你能试试吗?