jQuery:从字符串中删除重复项 [英] jQuery: remove duplicates from string
本文介绍了jQuery:从字符串中删除重复项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
有一个简单的方法我可以删除所有这些字符串的重复,以便每个值只在字符串中出现一次?
例如在上面的例子中,我想得到 val1,val2,val3,val4,val5 。
/ p>
解决方案
通过拆分字符串创建数组,并使用 unique()
尝试这样:
data = VAL1,VAL2,VAL3,VAL4,val5\" ;
arr = $ .unique(data.split(','));
data = arr.join(,); //用
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.
解决方案
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
这篇关于jQuery:从字符串中删除重复项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文