簡體   English   中英

如何使用 include function 作為變量值

[英]How to use the include function for a variable value

我想檢查一個字符串是否包含另一個來自 b 的字符串。

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

我應該怎么做?

它應該是:

 c = b.includes(a)

您要檢查的任何元素都假定為參數並includes對 Array 的方法調用。

這不是真正清楚你問,所以如果你要檢查,如果你的數組中的元素都在您的變量文本的一部分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)}`); }) 

Array.prototype.every可能是執行此操作的慣用方法:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM