首页 > 解决方案 > 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 !");
}

标签: javascriptarraysstringends-with

解决方案


怎么样Array.prototype.some()

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

推荐阅读