Reputation: 85
I was thinking about doing an array to make my current string.includes() || string.includes()...
code shorter, as checking for many words can make that list long, but I couldn't find an easy way to replicate this with array.
array.includes(string)
only seems to check is the string specifically that, so if there's something else in that string too it wont work, which is pretty much the opposite of what I'm trying to do.
Help much appreciated.
Upvotes: 0
Views: 56
Reputation: 1799
You could try this
const myString = "Hello";
const arrayOfStrings = ["H", "U", "W"];
const hasString = arrayOfStrings.some(el => myString.includes(el))
console.log(hasString)
Upvotes: 2
Reputation: 3043
It seems like you don't only want to make string.includes() || string.includes()...
shorter, but you also want to output all strings that are a substring as a collection, you can use map
and filter falsey
outputs, like this:
let array = ["ab", "a", "abc", "abd", "cd", "ce"];
let res = array.map(e=>"abcdef".includes(e)?e:null).filter(e=>e!=null)
console.log(res);
If you still want true/false output, you can use reduce, like this:
let array = ["ab", "a", "abc", "abd", "cd", "ce"];
let res = array.map(e=>"abcdef".includes(e)).reduce((a,b)=>a||b,false)
console.log(res);
Upvotes: 1