3

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 !");
}
4

1 回答 1

4

怎么样Array.prototype.some()

if (strings.some(s => 'hi, this is a test'.endsWith(s))) {...}
于 2020-04-20T22:48:50.520 回答