jQuery: remove duplicates from string [duplicate]

心不动则不痛 提交于 2019-11-28 11:51:13

问题


I have string values that contain a list of items separated with a comma, e.g. the value could be val1,val2,val3,val1,val4,val3,val2,val5.

Is there an easy way I can remove all duplicates from such a string so that each value only appears once within the string ?

E.g. in the above example I would like to get val1,val2,val3,val4,val5 instead.

Many thanks in advance, Tim.


回答1:


Make an array by split string and use unique() try like this:

 data = "val1,val2,val3,val4,val5";
 arr =  $.unique(data.split(','));
 data = arr.join(","); //get unique string back with 

live demo



来源:https://stackoverflow.com/questions/22127640/jquery-remove-duplicates-from-string

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!