Saturday, May 25, 2024
 Popular · Latest · Hot · Upcoming
84
rated 0 times [  90] [ 6]  / answers: 1 / hits: 14232  / 10 Years ago, sun, march 2, 2014, 12:00:00

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.


More From » jquery

 Answers
20

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


[#47248] Friday, February 28, 2014, 10 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
trayvon

Total Points: 35
Total Questions: 117
Total Answers: 88

Location: Guernsey
Member since Tue, Jul 6, 2021
3 Years ago
;