I have following array of objects.
[{"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}]
In which some records are antithesis ex. 1st element and 4th which has same rId, gId, and sId but disabled flag is opposite. I want to eliminate all such records.
My expected array is {"rId":24,"gId":39,"sId":18,"disabled":false} (eliminate all antithesis records)
I tried following code but it is giving me wrong output.
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;
}
})
})
Is there any elegant way to achieve expected output ?