Search code examples
javascriptarraysstringends-with

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

Solution

  • How about Array.prototype.some()?

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