Javascript多维isset()
运行时:Javascript多维isset(),javascript,jquery,Javascript,Jquery,运行时: if (data.custaccount.webaddress) { alert('found it'); } 我得到了错误 data.custaccount is undefined 我能绕过它的唯一方法似乎是使用多个这样的“如果”: if (undefined != data && undefined != data.custaccount && undefined != data.custaccount.webaddress) {
if (data.custaccount.webaddress) {
alert('found it');
}
我得到了错误
data.custaccount is undefined
我能绕过它的唯一方法似乎是使用多个这样的“如果”:
if (undefined != data && undefined != data.custaccount && undefined != data.custaccount.webaddress) {
alert('found it');
}
我有没有办法做得更简单一些
在php中,我们通常使用isset(data.custaccount.webaddress),这非常有效。javascript(或jquery)中是否有一个等价物
我们使用了try/catch,但发现这会大大降低脚本的性能
我曾看到其他人也在问类似的问题,但没有任何成功,但我希望stackoverflow能够完成这一天的正常工作:)
谢谢
Justin当然,在访问
data.custcount.webaddress
之前,您需要检查数据
是否已定义
你可以把支票写得更短一些
if(data && data.custaccount && data.custaccount.webaddress){
}
你需要检查一下
你说得对,使用try/catch
是不好的做法。另一种查找对象的方法是使用类似于
if('custaccount' in data && 'webaddress' in data.custaccount){
}
第一种方法看起来很明显,但功能不全:如果
data.custaccount.webaddress
返回错误值(false
,0
,”
,等等),它就不会执行它想要执行的操作。