如何对变量值使用include函数



我想检查一个字符串是否包括来自b.的另一个字符串。

a = "some variable value"
b = ["foo", "bar"] 
c = a.includes(b)

我该怎么做?

您所问的内容并不清楚,因此,如果您想检查数组中的元素是否是变量a中文本的一部分,则需要对b进行迭代,以验证每个元素是否都在a的字符串中,如下所示:

a = "some variable value foo"
b = ["foo", "bar"]
b.forEach(x => {
console.log(`The text '${x}' is in the text '${a}': ${a.includes(x)}`);
})

假设为:

c = b.includes(a)

无论您要检查的元素假设是一个参数,并且includes方法调用Array。

Array.prototype.every可能是实现这一点的惯用方法:

const a = "some variable value"
const b = ["foo", "bar"] 
const c = b.every(s => a.includes(s));

最新更新