[SOLVED] How to test if string ends with a list of string

Issue

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))) {...}

Answered By – Robo Robok

Answer Checked By – Marilyn (BugsFixing Volunteer)

Leave a Reply

Your email address will not be published. Required fields are marked *