Reputation: 153
I want to determine if a string from a list 'websites' is contained in another string 'url' & get the value of the string from the list as well.
The code below can determine if a string from 'websites' can be found within 'url', but fails to get back which string from the list triggered .some().
I get the following error: "ReferenceError: website is not defined"
Is there any way to get the value of the string from 'websites' after finding that 'url' contains some value of 'websites'?
websites = [
'google',
'youtube',
'twitter',
]
var url = window.location.href // e.g. returns www.google.com/soooomethingelse
if (websites.some(website => url.includes(website)))
{
console.log(website) // here I want to log 'google' from 'websites'
}
Upvotes: 1
Views: 735
Reputation: 1237
I think a lot easier would be to just use .find
method, which returns the element when the condition in the arrow function is met (arrow function returns true
).
websites = [
'google',
'youtube',
'twitter',
]
var url = window.location.href // e.g. returns www.google.com/soooomethingelse
var website = websites.find((el) => url.includes(el))
if (website) {
console.log(website) // here I want to log 'google' from 'websites'
}
Upvotes: 1