我的 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 5
indexof
方法,但是includes
ES 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`)