【发布时间】:2020-11-27 03:19:11
【问题描述】:
我有以下按预期工作的计算属性。如果 3 个数据属性中的任何一个为空字符串,我希望它返回 true,并且如果任何字符串为“n/a”,我希望它返回 true:
appNamesInvalid() {
if (!this.preferredAppName1 || !this.preferredAppName2 || !this.preferredAppName3) {
return true;
}
if (this.preferredAppName1.toLowerCase().includes('n/a') || this.preferredAppName2.toLowerCase().includes('n/a') || this.preferredAppName3.toLowerCase().includes('n/a')) {
return true;
}
return false;
},
我最初尝试这样做,但没有按预期工作,我不知道为什么:
appNamesInvalid() {
let appNames = [this.preferredAppName1, this.preferredAppName2, this.preferredAppName3];
appNames.forEach(appName => {
if (appName == "" || appName.toLowerCase().includes("n/a")) {
return true;
}
}
return false;
},
有没有更简洁的方法来重构第一个块中的工作代码?
【问题讨论】: