Check if a string contains any element of an array in JavaScript

It can be as simple as that:

const arr = ['banana', 'monkey banana', 'apple', 'kiwi', 'orange'];

const checker = value =>
  !['banana', 'apple'].some(element => value.includes(element));

console.log(arr.filter(checker));

ECMAScript 6 FTW!

The checker uses an arrow function.

The ! means that it will exclude all elements that doesn’t meet the conditions.

The some() method tests whether some element in the array passes the test implemented by the provided function.

from Array.prototype.some() docs on MDM

The includes() method determines whether one string may be found within another string, returning true or false as appropriate.

from String.prototype.includes() docs on MDM


As some latest ECMAScript features aren’t supported in all browsers, you should use Babel to compile your code to ECMAScript 5.

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)