Javascript比较从对象属性中获取的数字作为字符串

标签 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 restoreVal = String(selTechJobId);

alert("thisVal : " + thisVal + " | typeof thisVal : " + typeof thisVal);
alert("restoreVal : " + restoreVal + " | typeof restoreVal : " + typeof restoreVal);

alert("thisVal === restoreVal : " + thisVal === restoreVal);

当我运行此命令时,第三个警报显示“false”。对我来说,它显然应该显示“true”。我显然错过了一些东西。

我已经在谷歌上搜索了几个小时,我发现其中大部分都是关于类型不匹配问题。正如您所看到的,我将它们显式转换为 String,因此这不应该是这里的问题。

最佳答案

通过使用:"thisVal === RestoreVal : "+ thisVal,您正在连接字符串,因此您将 "thisVal === RestoreVal : 65"“65”

关于Javascript比较从对象属性中获取的数字作为字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26689531/

相关文章:

ruby-on-rails - 如何使 Rails 3 JSON 解析双引号字符串和单个数字

f# - 如何检查 F# 中的引用相等性?

javascript - 拖放排序元素后创建重复项

javascript - 如何在具有固定宽度和高度的可滚动 div 上创建模式覆盖?

json - Moshi自定义适配器将json镜像类转换为另一个

Javascript 数组 - 合并多个数组帮助

java - 如何在 Java 中比较字符串?

java - Android:当两个字符串相等时,在 onPostExecute() 中启动一个新的 Activity

javascript - 本地从canvas元素获取三个.js场景进行操作

javascript - 无法在 React.js 中导入 d3-queue?尝试导入错误 : 'queue' is not exported from 'd3' (imported as 'd3' )