jquery - Javascript remove duplicated object from array -
i'm having trouble remove duplicated object array
example:
var list = [{place:"aaa",name:"me"}, {place:"bbb",name:"you"}, {place:"aaa",name:"him"}];
in example have 3 objects, , want remove object have duplicated place
as added:
i want remove one, dont matter wich 1
if want remove duplicated items , keep first occcurence of particular place, can use simple loop re-create new array input:
var list = [{place:"aaa",name:"me"}, {place:"bbb",name:"you"}, {place:"aaa",name:"him"}]; var uniqplace = function(array){ var result = []; array.foreach(function(el){ if (result.filter(function(n){ return n.place === el.place }).length==0){ result.push(el); } }) return result; }
output:
uniqplace(list);
[{"place":"aaa","name":"me"},{"place":"bbb","name":"you"}]
Comments
Post a Comment