jquery - Remove element from array of objects javascript -
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 records antithesis ex. 1st element , 4th has same rid, gid, , sid disabled flag opposite. want eliminate such records.
my expected array {"rid":24,"gid":39,"sid":18,"disabled":false}
(eliminate antithesis records)
i tried following code 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 elegant way achieve expected output ?
you map()
, filter()
var data = [{"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}] var ar = data.map(function(e) { return e.rid + '|' + e.gid + '|' + e.sid; }); var result = data.filter(function(e) { var key = e.rid + '|' + e.gid + '|' + e.sid; return ar.indexof(key) == ar.lastindexof(key); }); console.log(result)
Comments
Post a Comment