I want to filter an array of urls by excluding ones that contain any substrings that are in a blacklist array.
const urls = [
'http://example.com/people/chuck',
'http://example.com/goats/sam',
'http://example.com/goats/billy',
'http://example.com/goats/linda',
'http://example.com/cows/mary',
'http://example.com/cows/betty',
'http://example.com/people/betty']
const blacklist = ['cows', 'goats']
let cleanUrls = [];
I can do this with for-loops but I want to find a clean/concise way using filter and/or reduce.
If I didn't need to loop over x number of blacklist items:
cleanUrls = urls.filter( url => !url.includes(blacklist[0]) )
.filter( url => !url.includes(blacklist[1]) )
I also don't want to just iterate through the blacklist with a forEach or map because I want to immediately stop if a particular url matches any blacklist entry.
Plain JS please. Thank you. :)
urls.some(url => blacklist.includes(url))