本文介绍了JavaScript .includes() 方法的多个条件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
只是想知道,有没有办法向 .includes 方法添加多个条件,例如:
Just wondering, is there a way to add multiple conditions to a .includes method, for example:
var value = str.includes("hello", "hi", "howdy");
想象一下逗号表示或".
Imagine the comma states "or".
它现在询问字符串是否包含 hello, hi 或 howdy.所以只有当一个,并且只有一个条件为真.
It's asking now if the string contains hello, hi or howdy. So only if one, and only one of the conditions is true.
有没有办法做到这一点?
Is there a method of doing that?
推荐答案
即使有一个也应该可行,并且只有一个条件为真:
That should work even if one, and only one of the conditions is true :
var str = "bonjour le monde vive le javascript";
var arr = ['bonjour','europe', 'c++'];
function contains(target, pattern){
var value = 0;
pattern.forEach(function(word){
value = value + target.includes(word);
});
return (value === 1)
}
console.log(contains(str, arr));
这篇关于JavaScript .includes() 方法的多个条件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!