所以我有两个条件的条件语句,其中
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执行代码的结果。
我该如何表达呢?
答案 0 :(得分:2)
我认为您只想使用两个可能同时运行的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
);
}
这里没有什么是异步的,也不涉及承诺。如果是这样,我建议只在各自的位置放置一个await
。
答案 1 :(得分:0)
您仍然需要等待操作,直到诺言得以解决并完成。因此,您将检查promise回调中的条件,然后采取适当的措施。在您解决诺言之前,您可以显示一些“正在加载”的信息。
答案 2 :(得分:0)
这里没有异步动作,因此无需跟踪带有承诺的异步动作。
可能最简单的方法是过滤已过滤列表:
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
。我觉得更清楚。
答案 3 :(得分:-1)
也许您想要此解决方案?
var dependency = new Dependency();
builder.RegisterInstance(dependency).As<Dependency>();
builder.RegisterType<ClassA>().As<IClassA>().SingleInstance();
builder.RegisterType<ClassB>().AsSelf().SingleInstance();