0

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

2 Answers 2

2

Try to switch array and string:

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

Sign up to request clarification or add additional context in comments.

3 Comments

maybe worth to mention, includes is implemented for array and string ...
And the output will be true not false.
if the string change to "helloaeeahwbdhbd", how can i find it?
1

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)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.