Sometimes,. we want to check if a string contains any element of an array in JavaScript.
In this article, we’ll look at how to check if a string contains any element of an array in JavaScript.
How to check if a string contains any element of an array in JavaScript?
To check if a string contains any element of an array in JavaScript, we call the array some
method.
For instance, we write
const arr = ["banana", "monkey banana", "apple", "kiwi", "orange"];
const checker = (value) =>
!["banana", "apple"].some((element) => value.includes(element));
console.log(arr.filter(checker));
to call arr.filter
with a callback that checks any of 'banana'
or 'apple'
isn’t in the value of element
being looped through in the arr
array.
As a result, filter
returns an array with values that don’t have 'banana'
or 'apple'
in arr
.
Conclusion
To check if a string contains any element of an array in JavaScript, we call the array some
method.