1

I have an string array with below value

const lists = ["EH-AA","EH-BB","EH-CC"]

I tried to use below code and I expect this statement return true.

lists.includes('EH-')

But it returns false actually.
How should I modify the condition statement so that it will return true

1
  • what is list? Is it a string that looks like an array, or is it an array containing 3 strings? Please edit your question to format that line of code because it changes the whole meaning of this question. Commented Nov 8, 2020 at 1:54

2 Answers 2

1

That's not how includes works, it checks the array for that exact value. You need to loop over each item in the list and check if that item has that substring. You can do that with .some:

lists.some(item => item.includes('EH-'))
Sign up to request clarification or add additional context in comments.

Comments

1

You could also join the array and then check if it includes the specified string.

const lists = ['EH-AA', 'EH-BB', 'EH-CC'];

const checkForStr = (arr, str) =>
   arr.join(',').includes(str);

console.log(checkForStr(lists, 'EH-'));
console.log(checkForStr(lists, 'random'));

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.