我正在使用下划线js的_.isFilter方法。如果我想检查2个JSON是否相同,它可以完美地工作。但是我面临着一个小问题。在一个JS中,我有$$ hashKey作为属性,而在其他它不存在。如何比较2个JSON的所有属性,除了一个,即$$ hashKey。
$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;
});
答案 0 :(得分:2)
您可以使用_.omit
功能在stringify
之前取出您不想比较的按键:
省略
_.omit(object, *keys)
返回对象的副本,过滤到 省略列入黑名单的键(或键组)。或者接受a 谓词指示要省略哪些键。
_.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
属性创建对象,然后进行比较。