Javascript 编写包含3个条件的if/else语句,并混合承诺
我有一个有两个条件的条件语句,其中Javascript 编写包含3个条件的if/else语句,并混合承诺,javascript,if-statement,react-native,es6-promise,Javascript,If Statement,React Native,Es6 Promise,我有一个有两个条件的条件语句,其中 let modItemList = this.props.items if (this.state.searchItemName) { // condition1 modItemList = ( this.props.items.filter( (item) => item.name.toLowerCase().indexOf(lcName) !== -1 //
let modItemList = this.props.items
if (this.state.searchItemName) { // condition1
modItemList = (
this.props.items.filter(
(item) => item.name.toLowerCase().indexOf(lcName) !== -1 // For name
)
);
} else if (this.state.searchItemAddress) { //condition2
modItemList = (
this.props.items.filter(
(item) => item.fullAddress.some(e => e.toLowerCase().indexOf(lcAddress) !== -1) // For Address
)
);
}
这是一个有点难解释的地方
现在我想添加第三个条件,只有当条件1和条件2都满足时才会发生,结果是从条件1和条件2执行代码
我该如何表达呢?也许这是您想要的解决方案
if (condition1 & condition2) {
something = this.props.something.filter(1)).then(this.props.something.filter(2)
} else if (condition1) {
something = this.props.something.filter(1)
} else if (condition2) {
something = this.props.something.filter(2)
}
你仍然需要等待行动,直到承诺得到解决和完成。因此,您需要检查promise回调中的条件,然后采取适当的措施。在解决promise之前,您可以显示一些“加载”信息。我认为您只需要使用两个单独的
if
条件,这两个条件都可以运行,而不是if
/else if
:
let modItemList = this.props.items;
if (this.state.searchItemName) { // condition1
modItemList = modItemList.filter(item =>
item.name.toLowerCase().indexOf(lcName) !== -1 // For name
);
}
if (this.state.searchItemAddress) { //condition2
modItemList = modItemList.filter(item =>
item.fullAddress.some(e => e.toLowerCase().indexOf(lcAddress) !== -1) // For Address
);
}
这里没有什么是异步的或涉及承诺的。如果有,我建议只在相应的位置放置一个
wait
。这里没有异步操作,因此不需要跟踪带有承诺的异步操作
可能最简单的方法是过滤过滤列表:
let modItemList = this.props.items;
if (this.state.searchItemName) {
modItemList = modItemList.filter(item => item.name.toLowerCase().includes(lcName));
}
if (this.state.searchItemAddress) {
modItemList = modItemList.filter(item => item.fullAddress.some(e => e.toLowerCase().includes(lcAddress)));
}
或者过滤一次,检查回调中的searchItemName
和searchItemAddress
:
let modItemList = this.props.items.filter(item =>
(!this.state.searchItemName || item.name.toLowerCase().includes(lcName)) &&
(!this.state.searchItemAddress || item.fullAddress.some(e => e.toLowerCase().includes(lcAddress));
即使名单上有几十万条条目,但这两条都不会慢到令人担忧的程度
或者,如果真的让您感到困扰,请执行双重筛选或重新检查,构建一个筛选函数:
let modItemList;
let filterFunc = null;
if (this.state.searchItemName && this.state.searchItemAddress) {
filterFunc = item => item.name.toLowerCase().includes(lcName) && item.fullAddress.some(e => e.toLowerCase().includes(lcAddress));
} else if (this.state.searchItemName) {
filterFunc = item => item.name.toLowerCase().includes(lcName);
} else if (this.state.searchItemAddress) {
filterFunc = item => item.fullAddress.some(e => e.toLowerCase().includes(lcAddress));
}
modItemList = filterFunc ? this.props.items.filter(filterFunc) : this.props.items;
不过,这需要重复一点,留下更新一个地址过滤器而不是另一个的可能性。您可以聚合过滤器函数:
let nameCheck = item => item.name.toLowerCase().includes(lcName);
let addressCheck = item => item.fullAddress.some(e => e.toLowerCase().includes(lcAddress));
let modItemList;
if (this.state.searchItemName && this.state.searchItemAddress) {
modItemList = this.props.items.filter(item => nameCheck(item) && addressCheck(item));
} else if (this.state.searchItemName) {
modItemList = this.props.items.filter(nameCheck);
} else if (this.state.searchItemAddress) {
modItemList = this.props.items.filter(addressCheck(item);
}
如果有两个以上,我们可以考虑将它们放入一个数组中
modItemList = this.props.items.filter(item => arrayOfFunctions.every(f => f(item)));
所以…有很多选择。:-)
我使用了
includes(x)
而不是indexOf(x)!=-1上面的代码。我觉得更清楚。恐怕你把事情说得太抽象了。:-)这段代码中的过滤器是什么?确实太抽象了。请回顾如何创建一个-我们无法非常有效地猜测您想要的是什么,因此一些实际演示此问题的代码将非常有用。啊,对了,抱歉。我将再次尝试用一个更好的codeHi示例重写@T.J.Crowder aha。这有点尴尬。事实上,我的答案是你昨天帮助我的。异步方面是什么?在这个问题上,你只是在过滤一个数组…然后是一个用于承诺的构造。筛选器将返回一个数组并将失败。这对解决我的问题有何帮助?@Reuben它会按照您的要求执行:如果两个条件都满足,那么两个筛选器都将应用。结果存储在modItemList
中。是的,您是对的。对不起,如果我的语气有点刺耳。如果你在的话,我会给你买杯啤酒。再次感谢您的帮助:-)很明显,我在试图弄明白这一点的时候想得太多了。