Home > OS >  .includes with array in Javascript
.includes with array in Javascript

Time:12-26

i wonder is there any way to check if in a string there are characters that match the characters in array?

const array = ["cake","hello","ok"];
const string = "hello"
let result = string.includes(array)
console.log(result)
// >false

CodePudding user response:

Try to switch array and string:

const array = ["cake","hello","ok"];
const string = "hello"
let result = array.includes(string)
console.log(result)

CodePudding user response:

I think you're looking for Array#some(): loop through the array and check if any of the elements match the predicate.

Here checking if string includes as a substring any of the strings in array.

const array = ["cake","hello","ok"];
const string = "helloaeeahwbdhbd"
let result = array.some(s => string.includes(s))
console.log(result)

  • Related