我可以简化以下 javascript 逻辑或代码吗?

Could I simplify the following javascript logical OR code?

我的项目中有一段代码,但它有点笨拙。谁能帮我简化下面的代码?

if (a == "good" || a == "beautiful || a == "pretty"|| a == "excellent" || a == "superb"|| a == "spectacular")

我可以把它做成某种数组,然后在这个 if 代码中使用那个数组吗?

我想您可以改用哈希集结构:在 JS 中,这是 Set(或在 TypeScript 中是 Set<T>):

const positiveWords = new Set( [ "good", "beautiful", "pretty", "excellent", "superb", "spectacular" ] );

用法:

if( positiveWords.has( a ) ) {
    
}

请注意,您需要先将 a 转换为小写。如果你想要不区分大小写(或不区分重音,或其他 culture/locale-specific 比较规则)then use Intl and/or localeCompare.

if (["good", "beautiful", "pretty", "excellent", "superb", "spectacular"].includes(a)) {...}