2014-10-28 51 views
0

我正在使用下劃線js的_.isFilter方法。如果我想檢查2個JSON是否相同,它會很好地工作。但我正面臨一個小問題。在一個JS中,我有$$ hashKey作爲屬性,而在其他情況下它不存在。我怎樣才能比較2個JSON的所有屬性,除了一個就是$$ hashKey。如何比較2個JSON的所有屬性,除了一個,使用UnderscoreJs?

$rootScope.jsonOfHRA = $scope.setFileNameAndOtherProperties($rootScope.jsonOfHRA); 
globalJsonHRA = JSON.parse(JSON.stringify($rootScope.jsonOfHRA)); 

//This is how I make a deep copy of JSON 

JSONOFHRA= 
{ 
    "toShowVerified": 0, 
    "sec10_decl_id": 390, 
    "toShowEdit": 0, 
    "toShowlineItemEditable": true, 
    "edit": false, 
    "sec10_decl_act_val": 0, 
    "sec10_decl_other_text1": "asdsa", 
    "sec10_decl_other_text2": "3434", 
    "sec10_decl_other_text3": "adasda", 
    "sec10_decl_decl_from_dt": "01/05/2014", 
    "sec10_decl_status": 0, 
    "sec10_decl_yr_end": "2015-03-31", 
    "sec10_decl_yr_id": 2014, 
    "listOfAttachments": [], 
    "nameOfPayheadToShow": "HRA", 
    "sec10_decl_proof": 0, 
    "sec10_decl_ver_val": 0, 
    "sec10_decl_yr_st": "2014-04-01", 
    "sec10_decl_cust_id": 315, 
    "add": false, 
    "sec10_decl_curr_id": 0, 
    "sec10_decl_mod_on": "2014-10-28 12:46:14.0", 
    "sec10_decl_add_on": "2014-10-28 12:38:49.0", 
    "sec10_decl_decl_to_dt": "30/06/2014", 
    "sec10_decl_user_id": 32967, 
    "sec10_decl_active": 0, 
    "sec10_decl_decl_val": 1234, 
    "sec10_decl_head_id": 3, 
    "sec10_decl_decl_text": "Delhi", 
    "$$hashKey": "19I" 
} 


GLOBALJSON = 
{ 
    "toShowVerified": 0, 
    "sec10_decl_id": 390, 
    "toShowEdit": 0, 
    "toShowlineItemEditable": true, 
    "edit": false, 
    "sec10_decl_act_val": 0, 
    "sec10_decl_other_text1": "asdsa", 
    "sec10_decl_other_text2": "3434", 
    "sec10_decl_other_text3": "adasda", 
    "sec10_decl_decl_from_dt": "01/05/2014", 
    "sec10_decl_status": 0, 
    "sec10_decl_yr_end": "2015-03-31", 
    "sec10_decl_yr_id": 2014, 
    "listOfAttachments": [], 
    "nameOfPayheadToShow": "HRA", 
    "sec10_decl_proof": 0, 
    "sec10_decl_ver_val": 0, 
    "sec10_decl_yr_st": "2014-04-01", 
    "sec10_decl_cust_id": 315, 
    "add": false, 
    "sec10_decl_curr_id": 0, 
    "sec10_decl_mod_on": "2014-10-28 12:46:14.0", 
    "sec10_decl_add_on": "2014-10-28 12:38:49.0", 
    "sec10_decl_decl_to_dt": "30/06/2014", 
    "sec10_decl_user_id": 32967, 
    "sec10_decl_active": 0, 
    "sec10_decl_decl_val": 1234, 
    "sec10_decl_head_id": 3, 
    "sec10_decl_decl_text": "Delhi" 
} 

//This is how I am comparing 
if(_.isEqual(modifiedJson[i], GlobalUnmodifiedJson[j])){ 
             var idOfUnmodifiedLineItem = modifiedJson[i]["sec10_decl_id"]; 
             alert("Coming here in realm of unmodified of modifed"); 
             modifiedJson = _.filter(modifiedJson, function(item) { 
              return item.id !== idOfUnmodifiedLineItem; 
             }); 

回答

2

可以使用_.omit功能取出鑰匙你不想以前stringify比較:

underscore.js docs

省略_.omit(object, *keys)
返回該對象的一個​​副本,過濾到 省略黑名單鍵(或鍵數組)。或者接受指示要省略哪些鍵的謂詞 。

_.omit({name: 'moe', age: 50, userid: 'moe1'}, 'userid'); 
=> {name: 'moe', age: 50} 

_.omit({name: 'moe', age: 50, userid: 'moe1'}, function(value, key, object) { 
    return _.isNumber(value); 
}); 
=> {name: 'moe', userid: 'moe1'} 

在你的情況,你可以創建從JSON,omit$$hashKey屬性的對象,然後進行比較。

相關問題