JavaScript .includes()方法的多个条件 [英] multiple conditions for JavaScript .includes() method

查看:2840
本文介绍了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");

想象逗号状态或
(它现在询问字符串是否包含hello hi 你好。所以只有当一个,只有一个条件成立时。

imagine the comma states "or" (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()方法的多个条件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆