我有以下對象數組。從對象數組中刪除元素javascript
[{"rId":24,"gId":40,"sId":20,"disabled":false},
{"rId":24,"gId":40,"sId":19,"disabled":false},
{"rId":24,"gId":40,"sId":50,"disabled":false},
{"rId":24,"gId":40,"sId":20,"disabled":true},
{"rId":24,"gId":40,"sId":19,"disabled":true},
{"rId":24,"gId":40,"sId":50,"disabled":true},
{"rId":24,"gId":39,"sId":18,"disabled":false}]
其中有些記錄是對立的ex。 1st元素和4th元素具有相同的rId,gId和sId,但禁用標誌是相反的。 我想消除所有這些記錄。
我的預期陣列是{"rId":24,"gId":39,"sId":18,"disabled":false}
(消除所有的對立面記錄)
我嘗試下面的代碼,但它給我錯誤的輸出。
arrOfObj=[{"rId":24,"gId":40,"sId":20,"disabled":false},
{"rId":24,"gId":40,"sId":19,"disabled":false},
{"rId":24,"gId":40,"sId":50,"disabled":false},
{"rId":24,"gId":40,"sId":20,"disabled":true},
{"rId":24,"gId":40,"sId":19,"disabled":true},
{"rId":24,"gId":40,"sId":50,"disabled":true},
{"rId":24,"gId":39,"sId":18,"disabled":false}]
$.each(arrOfObj,function (index1,firstObj) {
$.each(arrOfObj,function (index2,secondObj) {
if(index1>= index2){
return true;
}
var areObjAntithesis=firstObj.rId===secondObj.rId && firstObj.gId===secondObj.gId
&& firstObj.sId===secondObj.sId && firstObj.disabled!==secondObj.disabled;
if(areObjAntithesis){
arrOfObj.splice(index1,1);
arrOfObj.splice(index2,1)
return false;
}
})
})
是否有任何優雅的方式來實現預期的輸出?