我有,我想比較像下面一個複雜的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 –