【发布时间】:2016-05-24 19:11:14
【问题描述】:
所以我要反对:
var productCopy = {
"light": true,
"new": true,
"available": false
};
var model = {
"light": true,
"new": true,
"available": false
};
我需要比较两者,所以我这样做了:
// Create our 2 strings
var productCopyString = JSON.stringify(productCopy);
var modelString = JSON.stringify(model);
然后我这样做了:
return modelString !== productCopyString;
但它不起作用。所以我做了一个这样的console.log:
console.log('productCopy', productCopyString);
console.log('model', modelString);
console.log('are they different', modelString !== productCopyString)
然后返回:
productCopy {"light":true,"new":true,"available":false}
模型 {"light":true,"new":true,"available":false}
它们有什么不同吗
这很奇怪,所以我这样做了:
var productCopyString = angular.toJson(productCopy);
var modelString = angular.toJson(model);
我得到了同样的结果。
有人知道为什么吗?
【问题讨论】:
-
我不知道你在期待什么。字符串相等,所以
!==运算符给出false。 -
什么不起作用?我在 JSBin 中运行了它,结果符合预期。 jsbin.com/voqagoqawa/edit?js,console
-
有什么问题,你在期待什么,两个字符串是一样的,比较时在两者上应用了一个不相等的运算符,结果是假的,意思是,两者都是一样的,所以 what很奇怪?
-
are they different false这是一个真实的声明。
标签: javascript angularjs json