如何使用JS endswith()方法来检查字符串变量是否与数组中的任何一个项目结束?



我试过了,但是没有成功。

const tlds = [".com", ".net", ".it", ".edu", ".gov"]
ent/*ent is an existing variable in my code*/.endsWith(tlds)

我必须让它检查数组中的每一项吗?

是的,您必须使它检查数组中的每个项。您可以创建for循环来检查数组中的所有项。这是我的代码。

const tlds = [".com", ".net", ".it", ".edu", ".gov"]
function endsWith(string, end) {
for (let i = 0; i < tlds.length; i++) {
if (string.endsWith(end[i]) === true) return true
}
return false
}
endsWith(ent, tlds)

希望对你有所帮助(;

最新更新