我有,我想比较像下面一个复杂的JSON对象:比较两个对象与深度比较或与json.stringify?
$scope.new = [
{
"name": "Node-1",
"isParent": true,
"text" : [
{
"str" : "This is my first Node-1 string",
"parent":[]
},
{
"str" : "This is my second Node-1 string",
"parent":[]
}],
"nodes": [
{
"name": "Node-1-1",
"isParent": false,
"text" : [
{
"str" : "This is my first Node-1-1 string",
"parent":[]
},
{
"str" : "This is my second Node-1-1 string",
"parent":[]
}],
"nodes": [
{
"name": "Node-1-1-1",
"isParent": false,
"text" : [
{
"str" : "This is my first Node-1-1-1 string",
"parent":[]
},
{
"str" : "This is my second Node-1-1-1 string",
"parent":[]
}],
"nodes": []
}
]
}
]
}
]
但在比较我想也忽略1个属性,但因为我使用Angular.js我没有看到任何选项angular.equal
比较2个对象时将省略该属性。
console.log(angular.equals($scope.new,$scope.copy));
所以,在做研究,我想出了下面的回答这是使用lodash已经发出的选择,但问题是我想省略创建一个副本,我想我会在lodash的情况下性能下降。
Exclude some properties in comparison using isEqual() of lodash
所以,现在我想给对象转换这样的字符串,然后做对比,我想这将是快,但问题是,我怎么会忽略这个属性而字符串比较?
事情是这样的:
var str1 = JSON.stringify(JSON.stringify($scope.new));
var str2 = JSON.stringify(JSON.stringify($scope.copy));
console.log(str1==str2);
注:我想忽略isParent
的特性,同时比较2对象。
什么是比较2对象的最佳方法?
您是否想要忽略'isParent'只顶部对象,还是所有嵌套对象以及比较? –
@GuillermoMoratorio:在比较时,我想忽略所有嵌套对象中的isParent –