gpt4 book ai didi

javascript - 如何使用具有多个值的endsWith?

转载 作者:行者123 更新时间:2023-12-04 14:37:07 28 4
gpt4 key购买 nike

是否可以轻松地将 str.endsWith 与多个值一起使用?我有下一个代码:

var profileID = profile.id;
var abtest = profileID.endsWith("7");

{return abtest}
在这种情况下,我想检查是否 profileID以 a 结尾:1、3、5、7 或 9。只有当它为真时,我才希望 abtest 返回真。
我知道我可以这样做:
if (profileID.endsWith("1") || profileID.endsWith("3") || profileID.endsWith("5")
|| profileID.endsWith("7") || profileID.endsWith("9"))
{return abtest}
}
但如果可能的话,我想尝试一种更清洁的方式。有谁知道怎么做?

最佳答案

你可以试试 .some

if (['1','3','5','7','9'].some(char => profileID.endsWith(char))) {
//...
}

关于javascript - 如何使用具有多个值的endsWith?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63881200/

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