如何测试字符串是否以一串字符串列表结尾

5

我有一个字符串数组和另一个字符串,是否有一种简单的方法来测试我的单个字符串是否以字符串数组中的其中一个字符串结尾?

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 !");
}
1个回答

8

Array.prototype.some() 怎么样?

if (strings.some(s => 'hi, this is a test'.endsWith(s))) {...}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接