Reputation: 6461
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
Upvotes: 1
Views: 34
Reputation: 41893
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'));
Upvotes: 1
Reputation: 9063
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-'))
Upvotes: 1