How do I test for multiple .includes with Array.includes()
in my if statement? The below seems to work on one item, ie. slug-title-one
but ignores the second.
if (item.slug.includes('slug-title-one' || 'slug-title-13')) {
// checks for and displays if either
}
As the documentation the Array.includes
can only test for a single element.
You could reverse your logic though and use
if (['slug-title-one','slug-title-13'].some(slug => item.slug.includes(slug))) {
// checks for and displays if either
}
You have a few options. Here are some ideas:
Repeat includes
if (item.slug.includes('slug-title-one') || item.slug.includes('slug-title-13')) { ... }
Helper function
if( includes(item.slug, 'slug-title-one', 'slug-title-13') ) { ... }
function includes() {
var args = Array.prototype.slice.call(arguments);
var target = args[0];
var strs = args.slice(1); // remove first element
for(var i = 0; i < strs.length; i++) {
if(target.includes(strs[i])) {
return true;
}
}
return false;
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.