Javascript 无法确定angular2+;中的Json键中是否包含字符串;
我的json密钥是这样的- .items.properties.Client Code.type中的虚拟帐号验证:“数组” 我想确定上述json密钥中是否存在“客户机代码” 我试着用-Javascript 无法确定angular2+;中的Json键中是否包含字符串;,javascript,angular,Javascript,Angular,我的json密钥是这样的- .items.properties.Client Code.type中的虚拟帐号验证:“数组” 我想确定上述json密钥中是否存在“客户机代码” 我试着用- var abc = "Virtual Account Number Verification IN.items.properties.Client Code.type" var pqr = "Client Code" abc.includes("." +pqr+".") 但它不起作用。 其他解决方法是什么
var abc = "Virtual Account Number Verification IN.items.properties.Client Code.type"
var pqr = "Client Code"
abc.includes("." +pqr+".")
但它不起作用。
其他解决方法是什么?您的代码可以按预期工作。它们之间的区别是
es5
具有indexof
方法,而包含则来自es6
如果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`)
的另一种方式包括
:
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`)
为什么它不工作,看看这里。检查您的控制台
可能是其他情况
var abc=“虚拟账号验证IN.items.properties.Client code.type”
var pqr=“客户代码”
控制台日志(“.”+pqr+”)
控制台日志(abc.包括(“.”+pqr+”)代码>var yesItDoesWork=abc.包括(“.”+pqr+”)代码>-现在。。。yesItDoesWork
的值是多少。。。你写的代码是有效的,只是你放弃了结果你说“不起作用”是什么意思?您的代码段返回true,这意味着它正在按预期工作。该代码段在@AkashSrivastavindexOf
is es5时不返回任何内容,es6已有4年以上的历史,es6中定义了.includes
-此代码甚至没有将pqr
与
包围在一起,因此可能返回falsepositives@VLAZOP的代码按预期工作。他们之间的区别是ES5有indexof
方法,而includes
是在ES6中。@downvoter请告诉我什么是downvote的原因?如果能改进我的回答,那将非常有帮助。提高答案对我来说真的很重要。