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.

halfer
  • 19,824
  • 17
  • 99
  • 186
Dyvel
  • 847
  • 1
  • 8
  • 20
  • 2
    `test.some(str => str.includes('name'))` –  Jan 15 '21 at 10:25
  • @ChrisG works perfect. Thanks :-) Please provide an answer so I can credit you – Dyvel Jan 15 '21 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 :) –  Jan 15 '21 at 10:47

0 Answers0