javascript - How to test if string ends with a list of string
问题描述
I have an array of string and another string, Is there a simple way to test if my single string ends with one of the strings in the array of strings?
const strings = [
'foo',
'bar',
'test'
];
if('hi, this is a test'.endsWith(...strings)) { // I know that this isn't right but this is the idea
console.log("String ends with one of the strings !");
}
解决方案
怎么样Array.prototype.some()
?
if (strings.some(s => 'hi, this is a test'.endsWith(s))) {...}
推荐阅读
- awk - 使用 awk 在列数不等的表中打印长度为 32 个字符的条目
- sapui5 - ODataModel.resetChanges 错误:“无法读取未定义的属性 'then'”
- java - XSD 错误:cvc-complex-type.2.4.a:发现无效内容以元素开头
- python - 当“param”和“df1”对应变量的组合匹配时,替换旨在将 df['value'] 乘以 param['factor'] 的 FOR 循环
- python - 将带有条形图的样式化 pandas df 导出为 png
- java - 查找整数数组的平均值
- python - 理解 [] 符号
- python - 将python转换为SML
- eclipse - 无法在 Eclipse Git Remote Tracking 中为一台机器获取新分支
- excel - VBA:带双引号的公式