gpt4 book ai didi

Javascript:缩短 if ( ... or ...) 条件

转载 作者:行者123 更新时间:2023-12-03 20:30:42 32 4
gpt4 key购买 nike

if (args.join(" ").toLowerCase() === "are you" || args.join(" ").toLowerCase() === "are you doing")

正如您所看到的,我使用了两个非常相似的行和一个 or 大小写。有没有更短的方法来做到这一点?

最佳答案

["are you", "are you doing"].includes(args.join(" ").toLowerCase())

此解决方案的好处是 args.join("").toLowerCase() 仅执行一次,并且它不冗长,同时仍然具有表达力且易于理解。
它也很容易扩展。如果您有更多要验证的字符串,只需将这些字符串添加到数组即可。

关于Javascript:缩短 if ( ... or ...) 条件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50875221/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com