0

I'm trying to test if an array contains a specific substring, but I can only get a match if the substring = array value

This is my test code:

var test = ["something", "my-name", "name-test"];
if (test.includes('name')) {
  console.log(test);
}

I've also tried with indexOf('navn') !== -1 and jQuery inArray.

However, no matter what I try, it only works if I my substring == array value, but I need 'name' to be 'true' when array value is e.g. 'my-name' or 'name-test'.

It seems so simple, yet I can't get it to work.

3
  • 2
    test.some(str => str.includes('name'))
    – user5734311
    Commented Jan 15, 2021 at 10:25
  • @ChrisG works perfect. Thanks :-) Please provide an answer so I can credit you
    – Dyvel
    Commented Jan 15, 2021 at 10:46
  • Glad it works, however I'm not here for cheap points grabbing so won't post an answer. This a dupe anyway :)
    – user5734311
    Commented Jan 15, 2021 at 10:47

0

Browse other questions tagged or ask your own question.