javascript - How can I remove duplication from list -
i have kind of list web service. want eliminate username duplication:
mylist = [{ "username": "plr1", "is_online": true, "email": null, "message": null, "direction": 1, "image_url": "" }, { "username": "plr2", "is_online": false, "email": "", "message": null, "direction": 1, "image_url": "" }, { "username": "plr1", "is_online": false, "email": "", "message": null, "direction": 1, "image_url": null }]; is there function allows me remove duplicated values (one of elements=plr1)?
demo: http://jsfiddle.net/abc123/zgybb/
note: last loop show items removed , left in array.
js:
var mylist= [{"username":"plr1","is_online":true,"email":null,"message":null,"direction":1,"image_url":""},{"username":"plr2","is_online":false,"email":"","message":null,"direction":1,"image_url":""},{"username":"plr1","is_online":false,"email":"","message":null,"direction":1,"image_url":null} ]; (var = 0; < mylist.length; i++) { for(var j = + 1; j < mylist.length; j++) { if(mylist[i].username.tolowercase() == mylist[j].username.tolowercase()) mylist.splice(j, 1); } } //not needed proof items removed. (var = 0; < mylist.length; i++) { alert(mylist[i].username); }
Comments
Post a Comment