Javascript将从对象属性获取的数字作为字符串进行比较
我陷入了一个奇怪的境地,从服务器接收到的JSON的一个数字属性没有通过简单的相等性测试,这让我陷入了数小时Javascript将从对象属性获取的数字作为字符串进行比较,javascript,json,equality,Javascript,Json,Equality,我陷入了一个奇怪的境地,从服务器接收到的JSON的一个数字属性没有通过简单的相等性测试,这让我陷入了数小时 var form = {'answer':'','categoryDisplay':'dbAdmin','creationDate':null,'id':0,'question':'','techJobDisplay':null,'techJobId':65}; var selTechJobId = form.techJobId; var thisVal = String(65);
var form = {'answer':'','categoryDisplay':'dbAdmin','creationDate':null,'id':0,'question':'','techJobDisplay':null,'techJobId':65};
var selTechJobId = form.techJobId;
var thisVal = String(65);
var restoreVal = String(selTechJobId);
alert("thisVal : " + thisVal + " | typeof thisVal : " + typeof thisVal);
alert("restoreVal : " + restoreVal + " | typeof restoreVal : " + typeof restoreVal);
alert("thisVal === restoreVal : " + thisVal === restoreVal);
当我运行此命令时,第三个警报显示“false”。对我来说,它应该清楚地显示“真”。我显然错过了什么
我已经在谷歌上搜索了几个小时,我发现大多数都是关于类型不匹配的问题。正如您所看到的,我显式地将它们都转换为字符串,所以这里不应该有问题。通过使用:“thisVal===restoreVal:”+thisVal
您正在合并字符串,因此您正在使用:“thisVal==restoreVal:65”
与“65”进行比较:“thisVal==restoreVal:”+thisVal
您正在合并字符串,因此您正在将“thisVal===restoreVal:65”
与“65”
进行比较,方法是使用:“thisVal==restoreVal:”+thisVal
您正在合并字符串,因此您正在将“thisVal===restoreVal:65”
与“65”
进行比较,方法是:“thisVal===restoreVal:“+thisVal
您正在连接字符串,因此您正在将”thisVal===restoreVal:65“
与“65”
修复您的代码
alert("thisVal === restoreVal : " + (thisVal === restoreVal));
因为在您的变体中,您比较了“thisVal===restoreVal:65”和“65”-它们不相等
修复您的代码
alert("thisVal === restoreVal : " + (thisVal === restoreVal));
因为在您的变体中,您比较了“thisVal===restoreVal:65”和“65”-它们不相等
修复您的代码
alert("thisVal === restoreVal : " + (thisVal === restoreVal));
因为在您的变体中,您比较了“thisVal===restoreVal:65”和“65”-它们不相等
修复您的代码
alert("thisVal === restoreVal : " + (thisVal === restoreVal));
因为在您的变体中,您比较了“thisVal===restoreVal:65”和“65”-它们不相等
问题发生在代码中的最后一个警报中,您必须在语句周围插入括号
在+运算符之后,如下所示:
var form = {'answer':'','categoryDisplay':'dbAdmin','creationDate':null,'id':0,'question':'','techJobDisplay':null,'techJobId':65};
var selTechJobId = form.techJobId;
var thisVal = String(65);
var restoreVal = String(selTechJobId);
alert("thisVal : " + thisVal + " | typeof thisVal : " + typeof thisVal);
alert("restoreVal : " + restoreVal + " | typeof restoreVal : " + typeof restoreVal);
alert("thisVal === restoreVal : " + (thisVal === restoreVal));
这样,它就不会在之前尝试将表达式的第一部分的值添加到thisVal
计算它时问题发生在代码中的最后一个警报中,您必须在语句周围加括号
在+运算符之后,如下所示:
var form = {'answer':'','categoryDisplay':'dbAdmin','creationDate':null,'id':0,'question':'','techJobDisplay':null,'techJobId':65};
var selTechJobId = form.techJobId;
var thisVal = String(65);
var restoreVal = String(selTechJobId);
alert("thisVal : " + thisVal + " | typeof thisVal : " + typeof thisVal);
alert("restoreVal : " + restoreVal + " | typeof restoreVal : " + typeof restoreVal);
alert("thisVal === restoreVal : " + (thisVal === restoreVal));
这样,它就不会在之前尝试将表达式的第一部分的值添加到thisVal
计算它时问题发生在代码中的最后一个警报中,您必须在语句周围加括号
在+运算符之后,如下所示:
var form = {'answer':'','categoryDisplay':'dbAdmin','creationDate':null,'id':0,'question':'','techJobDisplay':null,'techJobId':65};
var selTechJobId = form.techJobId;
var thisVal = String(65);
var restoreVal = String(selTechJobId);
alert("thisVal : " + thisVal + " | typeof thisVal : " + typeof thisVal);
alert("restoreVal : " + restoreVal + " | typeof restoreVal : " + typeof restoreVal);
alert("thisVal === restoreVal : " + (thisVal === restoreVal));
这样,它就不会在之前尝试将表达式的第一部分的值添加到thisVal
计算它时问题发生在代码中的最后一个警报中,您必须在语句周围加括号
在+运算符之后,如下所示:
var form = {'answer':'','categoryDisplay':'dbAdmin','creationDate':null,'id':0,'question':'','techJobDisplay':null,'techJobId':65};
var selTechJobId = form.techJobId;
var thisVal = String(65);
var restoreVal = String(selTechJobId);
alert("thisVal : " + thisVal + " | typeof thisVal : " + typeof thisVal);
alert("restoreVal : " + restoreVal + " | typeof restoreVal : " + typeof restoreVal);
alert("thisVal === restoreVal : " + (thisVal === restoreVal));
这样,它就不会在之前尝试将表达式的第一部分的值添加到thisVal
评估它Geez。当你饿的时候真的不适合编码。非常感谢!5分钟后会接受。当你饿的时候真的不适合编码。非常感谢!5分钟后会接受。当你饿的时候真的不适合编码。非常感谢!5分钟后会接受。当你饿的时候真的不适合编码。非常感谢!会吗5分钟后进行ept