无法确定 angular2+ 中的 Json 键中是否包含字符串



我的 json 键是这样的 -

"虚拟帐号验证 IN.items.properties.Client Code.type" : "array">

我想确定上面的 json 键中是否存在"客户端代码"。

我尝试使用 -

var abc = "Virtual Account Number Verification IN.items.properties.Client Code.type"
var pqr =  "Client Code"
abc.includes("." +pqr+".")

但它不起作用。 其他解决方法是什么?

为什么它不起作用,看看这里。检查您的console可能是其他情况。

var abc = "Virtual Account Number Verification IN.items.properties.Client Code.type"
var pqr =  "Client Code"
console.log("." +pqr+".")
console.log(abc.includes("." +pqr+"."));

true搜索字符串是否在给定字符串中的任何位置找到;否则,如果不是,则false.about 包含和浏览器支持

您的代码按预期工作。它们之间的区别,ES 5indexof方法,但是includesES 6

。如果abc是字符串类型,则可以使用以下代码片段:

var abc = "Virtual Account Number Verification IN.items.properties.Client Code.type";
var pqr =  "Client Code";
var isContains = abc.indexOf(`.${pqr}.`);
if(isContains != -1)
console.log(`There is a client code`)
else 
console.log(`There is no a client code`)

另一种includes方式:

var abc = "Virtual Account Number Verification IN.items.properties.Client Code.type";
var pqr =  "Client Code";
if(abc.includes(`.${pqr}.`))
console.log(`There is a client code`)
else 
console.log(`There is no a client code`)

最新更新