4

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 !");
}
Ayfri
  • 570
  • 1
  • 4
  • 24

1 Answers1

6

How about Array.prototype.some()?

if (strings.some(s => 'hi, this is a test'.endsWith(s))) {...}
Robo Robok
  • 21,132
  • 17
  • 68
  • 126