Take the following string as an example:
以以下字符串为例:
var string = "spanner, span, spaniel, span";
From this string I would like to find the duplicate words, remove all the duplicates keeping one occurrence of the word in place and then output the revised string.
从这个字符串中,我希望找到重复的单词,删除所有重复的单词,保持一个单词的出现,然后输出修改后的字符串。
Which in this example would be:
在这个例子中应该是:
var string = "spanner, span, spaniel";
I've setup a jsFiddle for testing: http://jsfiddle.net/p2Gqc/
我已经设置了一个用于测试的jsFiddle: http://jsfiddle.net/p2Gqc/
Note that the order of the words in the string is not consistent, neither is the length of each string so a regex isn't going to do the job here I don't think. I'm thinking something along the lines of splitting the string into an array? But I'd like it to be as light on the client as possible and super speedy...
注意,字符串中单词的顺序不一致,每个字符串的长度也不一致,所以我认为正则表达式在这里不会起作用。我在想把字符串分割成数组?但我希望它能尽可能轻的客户和超级迅速……
9 个解决方案
#1
32
How about something like this?
像这样的东西怎么样?
split the string, get the array, filter it to remove duplicate items, join them back.
分割字符串,获取数组,过滤它以删除重复的项,并将它们连接回去。
var uniqueList=string.split(',').filter(function(item,i,allItems){
return i==allItems.indexOf(item);
}).join(',');
$('#output').append(uniqueList);
Fiddle
For non supporting browsers you can tackle it by adding this in your js.
对于不支持的浏览器,您可以通过在您的js中添加它来解决这个问题。
See Filter
看到过滤器
if (!Array.prototype.filter)
{
Array.prototype.filter = function(fun /*, thisp*/)
{
"use strict";
if (this == null)
throw new TypeError();
var t = Object(this);
var len = t.length >>> 0;
if (typeof fun != "function")
throw new TypeError();
var res = [];
var thisp = arguments[1];
for (var i = 0; i < len; i++)
{
if (i in t)
{
var val = t[i]; // in case fun mutates this
if (fun.call(thisp, val, i, t))
res.push(val);
}
}
return res;
};
}
#2
2
If non of the above works for you here is another way:
如果以上这些对你有帮助的话,这是另一种方式:
var str = "spanner, span, spaniel, span";
str = str.replace(/[ ]/g,"").split(",");
var result = [];
for(var i =0; i < str.length ; i++){
if(result.indexOf(str[i]) == -1) result.push(str[i]);
}
result=result.join(", ");
Or if you want it to be in a better shape try this:
或者,如果你想让它有更好的形状,试试这个:
Array.prototype.removeDuplicate = function(){
var result = [];
for(var i =0; i < this.length ; i++){
if(result.indexOf(this[i]) == -1) result.push(this[i]);
}
return result;
}
var str = "spanner, span, spaniel, span";
str = str.replace(/[ ]/g,"").split(",").removeDuplicate().join(", ");
#3
1
Both the other answers would work fine, although the filter
array method used by PSL was added in ECMAScript 5 and won't be available in old browsers.
尽管PSL使用的filter数组方法在ECMAScript 5中添加了,但在旧浏览器中无法使用,但是其他的答案都可以正常工作。
If you are handling long strings then using $.inArray
/Array.indexOf
isn't the most efficient way of checking if you've seen an item before (it would involve scanning the whole array each time). Instead you could store each word as a key in an object and take advantage of hash-based look-ups which will be much faster than reading through a large array.
如果处理长字符串,则使用$. inarray /数组。indexOf并不是最有效的检查项目的方法(每次都要扫描整个数组)。相反,您可以将每个单词作为键存储在对象中,并利用基于散列的查找,这将比在大数组中读取要快得多。
var tmp={};
var arrOut=[];
$.each(string.split(', '), function(_,word){
if (!(word in tmp)){
tmp[word]=1;
arrOut.push(word);
}
});
arrOut.join(', ');
#4
1
<script type="text/javascript">
str=prompt("Enter String::","");
arr=new Array();
arr=str.split(",");
unique=new Array();
for(i=0;i<arr.length;i++)
{
if((i==arr.indexOf(arr[i]))||(arr.indexOf(arr[i])==arr.lastIndexOf(arr[i])))
unique.push(arr[i]);
}
unique.join(",");
alert(unique);
</script>
this code block will remove duplicate words from a sentence.
这个代码块将从句子中删除重复的单词。
the first condition of if statement i.e (i==arr.indexOf(arr[i])) will include the first occurence of a repeating word to the result(variale unique in this code).
if语句i的第一个条件。e (i== =arr. indexof (arr[i]))将包含重复单词的第一次出现(此代码中唯一的变量)。
the second condition (arr.indexOf(arr[i])==arr.lastIndexOf(arr[i])) will include all non repeating words.
第二个条件(arr. indexof (arr[i])= arr. lastindexof (arr[i])将包括所有非重复的词。
#5
0
// Take the following string
var string = "spanner, span, spaniel, span";
var arr = string.split(", ");
var unique = [];
$.each(arr, function (index,word) {
if ($.inArray(word, unique) === -1)
unique.push(word);
});
alert(unique);
现场演示
#6
0
below is an easy to understand and quick code to remove duplicate words in a string:
下面是一个易于理解的快速代码,用于删除字符串中的重复单词:
var string = "spanner, span, spaniel, span";
var uniqueListIndex=string.split(',').filter(function(currentItem,i,allItems){
return (i == allItems.indexOf(currentItem));
});
var uniqueList=uniqueListIndex.join(',');
alert(uniqueList);//Result:spanner, span, spaniel
As simple as this can solve your problem. Hope this helps. Cheers :)
就这么简单就能解决你的问题。希望这个有帮助。欢呼:)
#7
0
To delete all duplicate words, I use this code:
为了删除所有重复的单词,我使用以下代码:
<script>
function deleteDuplicate(a){a=a.toString().replace(/ /g,",");a=a.replace(/[ ]/g,"").split(",");for(var b=[],c=0;c<a.length;c++)-1==b.indexOf(a[c])&&b.push(a[c]);b=b.join(", ");return b=b.replace(/,/g," ")};
document.write(deleteDuplicate("g g g g"));
</script>
#8
0
Alternate Solution using Regular Expression
By making use of positive lookahead, you can strip off all the duplicate words.
通过使用积极的前视,你可以去掉所有重复的单词。
Regex /(\b\S+\b)(?=.*\1)/ig
, where
正则表达式/(\ b \ S + \ b)(? = . * \ 1)/搞笑
-
\b
- matches word boundary - \b -匹配单词边界
-
\S
- matches character that is not white space(tabs, line breaks,etc) - \S -匹配非空白字符(制表符、换行符等)
-
?=
- used for positive lookahead - -用于积极的前瞻
-
ig
- flags for in-casesensitive,global search respectively - 用于全局搜索的全局标记
-
+,*
- quantifiers. + -> 1 or more, * -> 0 or more - +,*——量词。+ -> 1或更多,* -> 0或更多
-
()
- define a group - () -定义一个组
-
\1
- back-reference to the results of the previous group - \1 -参照前一组的结果
var string1 = 'spanner, span, spaniel, span';
var string2 = 'spanner, span, spaniel, span, span';
var string3 = 'What, the, the, heck';
// modified regex to remove preceding ',' and ' ' as per your scenario
var result1 = string1.replace(/(\b, \w+\b)(?=.*\1)/ig, '');
var result2 = string2.replace(/(\b, \w+\b)(?=.*\1)/ig, '');
var result3 = string3.replace(/(\b, \w+\b)(?=.*\1)/ig, '');
console.log(string1 + ' => ' + result1);
console.log(string2 + ' => ' + result2);
console.log(string3 + ' => ' + result3);
The only caveat is that this regex keeps only the last instance of a found duplicate word and strips off all the rest. To those who care only about duplicates and not about the order of the words, this should work!
唯一要注意的是,这个regex只保留已找到的重复单词的最后一个实例,并删除所有其余的。对于那些只关心重复而不关心单词顺序的人来说,这应该是可行的!
#9
-1
var string = "spanner, span, spaniel, span";
var strArray= string.split(",");
var unique = [];
for(var i =0; i< strArray.length; i++)
{
eval(unique[strArray] = new Object());
}
//You can easily traverse the unique through foreach.
//你可以很容易地遍历唯一的foreach。
I like this for three reason. First, it works with IE8 or any other browser.
我喜欢这个有三个原因。首先,它与IE8或其他浏览器兼容。
Second. it is more optimized and guaranteed to have unique result.
第二。它更加优化,并且保证有独特的结果。
Last, It works for Other String array which has White space in their inputs like
最后,它适用于其他在输入中有空格的字符串数组
var string[] = {"New York", "New Jersey", "South Hampsire","New York"};
for the above case there will be only three elements in the string[] which would be uniquely stored.
对于上述情况,字符串[]中只有三个元素是惟一存储的。
#1
32
How about something like this?
像这样的东西怎么样?
split the string, get the array, filter it to remove duplicate items, join them back.
分割字符串,获取数组,过滤它以删除重复的项,并将它们连接回去。
var uniqueList=string.split(',').filter(function(item,i,allItems){
return i==allItems.indexOf(item);
}).join(',');
$('#output').append(uniqueList);
Fiddle
For non supporting browsers you can tackle it by adding this in your js.
对于不支持的浏览器,您可以通过在您的js中添加它来解决这个问题。
See Filter
看到过滤器
if (!Array.prototype.filter)
{
Array.prototype.filter = function(fun /*, thisp*/)
{
"use strict";
if (this == null)
throw new TypeError();
var t = Object(this);
var len = t.length >>> 0;
if (typeof fun != "function")
throw new TypeError();
var res = [];
var thisp = arguments[1];
for (var i = 0; i < len; i++)
{
if (i in t)
{
var val = t[i]; // in case fun mutates this
if (fun.call(thisp, val, i, t))
res.push(val);
}
}
return res;
};
}
#2
2
If non of the above works for you here is another way:
如果以上这些对你有帮助的话,这是另一种方式:
var str = "spanner, span, spaniel, span";
str = str.replace(/[ ]/g,"").split(",");
var result = [];
for(var i =0; i < str.length ; i++){
if(result.indexOf(str[i]) == -1) result.push(str[i]);
}
result=result.join(", ");
Or if you want it to be in a better shape try this:
或者,如果你想让它有更好的形状,试试这个:
Array.prototype.removeDuplicate = function(){
var result = [];
for(var i =0; i < this.length ; i++){
if(result.indexOf(this[i]) == -1) result.push(this[i]);
}
return result;
}
var str = "spanner, span, spaniel, span";
str = str.replace(/[ ]/g,"").split(",").removeDuplicate().join(", ");
#3
1
Both the other answers would work fine, although the filter
array method used by PSL was added in ECMAScript 5 and won't be available in old browsers.
尽管PSL使用的filter数组方法在ECMAScript 5中添加了,但在旧浏览器中无法使用,但是其他的答案都可以正常工作。
If you are handling long strings then using $.inArray
/Array.indexOf
isn't the most efficient way of checking if you've seen an item before (it would involve scanning the whole array each time). Instead you could store each word as a key in an object and take advantage of hash-based look-ups which will be much faster than reading through a large array.
如果处理长字符串,则使用$. inarray /数组。indexOf并不是最有效的检查项目的方法(每次都要扫描整个数组)。相反,您可以将每个单词作为键存储在对象中,并利用基于散列的查找,这将比在大数组中读取要快得多。
var tmp={};
var arrOut=[];
$.each(string.split(', '), function(_,word){
if (!(word in tmp)){
tmp[word]=1;
arrOut.push(word);
}
});
arrOut.join(', ');
#4
1
<script type="text/javascript">
str=prompt("Enter String::","");
arr=new Array();
arr=str.split(",");
unique=new Array();
for(i=0;i<arr.length;i++)
{
if((i==arr.indexOf(arr[i]))||(arr.indexOf(arr[i])==arr.lastIndexOf(arr[i])))
unique.push(arr[i]);
}
unique.join(",");
alert(unique);
</script>
this code block will remove duplicate words from a sentence.
这个代码块将从句子中删除重复的单词。
the first condition of if statement i.e (i==arr.indexOf(arr[i])) will include the first occurence of a repeating word to the result(variale unique in this code).
if语句i的第一个条件。e (i== =arr. indexof (arr[i]))将包含重复单词的第一次出现(此代码中唯一的变量)。
the second condition (arr.indexOf(arr[i])==arr.lastIndexOf(arr[i])) will include all non repeating words.
第二个条件(arr. indexof (arr[i])= arr. lastindexof (arr[i])将包括所有非重复的词。
#5
0
// Take the following string
var string = "spanner, span, spaniel, span";
var arr = string.split(", ");
var unique = [];
$.each(arr, function (index,word) {
if ($.inArray(word, unique) === -1)
unique.push(word);
});
alert(unique);
现场演示
#6
0
below is an easy to understand and quick code to remove duplicate words in a string:
下面是一个易于理解的快速代码,用于删除字符串中的重复单词:
var string = "spanner, span, spaniel, span";
var uniqueListIndex=string.split(',').filter(function(currentItem,i,allItems){
return (i == allItems.indexOf(currentItem));
});
var uniqueList=uniqueListIndex.join(',');
alert(uniqueList);//Result:spanner, span, spaniel
As simple as this can solve your problem. Hope this helps. Cheers :)
就这么简单就能解决你的问题。希望这个有帮助。欢呼:)
#7
0
To delete all duplicate words, I use this code:
为了删除所有重复的单词,我使用以下代码:
<script>
function deleteDuplicate(a){a=a.toString().replace(/ /g,",");a=a.replace(/[ ]/g,"").split(",");for(var b=[],c=0;c<a.length;c++)-1==b.indexOf(a[c])&&b.push(a[c]);b=b.join(", ");return b=b.replace(/,/g," ")};
document.write(deleteDuplicate("g g g g"));
</script>
#8
0
Alternate Solution using Regular Expression
By making use of positive lookahead, you can strip off all the duplicate words.
通过使用积极的前视,你可以去掉所有重复的单词。
Regex /(\b\S+\b)(?=.*\1)/ig
, where
正则表达式/(\ b \ S + \ b)(? = . * \ 1)/搞笑
-
\b
- matches word boundary - \b -匹配单词边界
-
\S
- matches character that is not white space(tabs, line breaks,etc) - \S -匹配非空白字符(制表符、换行符等)
-
?=
- used for positive lookahead - -用于积极的前瞻
-
ig
- flags for in-casesensitive,global search respectively - 用于全局搜索的全局标记
-
+,*
- quantifiers. + -> 1 or more, * -> 0 or more - +,*——量词。+ -> 1或更多,* -> 0或更多
-
()
- define a group - () -定义一个组
-
\1
- back-reference to the results of the previous group - \1 -参照前一组的结果
var string1 = 'spanner, span, spaniel, span';
var string2 = 'spanner, span, spaniel, span, span';
var string3 = 'What, the, the, heck';
// modified regex to remove preceding ',' and ' ' as per your scenario
var result1 = string1.replace(/(\b, \w+\b)(?=.*\1)/ig, '');
var result2 = string2.replace(/(\b, \w+\b)(?=.*\1)/ig, '');
var result3 = string3.replace(/(\b, \w+\b)(?=.*\1)/ig, '');
console.log(string1 + ' => ' + result1);
console.log(string2 + ' => ' + result2);
console.log(string3 + ' => ' + result3);
The only caveat is that this regex keeps only the last instance of a found duplicate word and strips off all the rest. To those who care only about duplicates and not about the order of the words, this should work!
唯一要注意的是,这个regex只保留已找到的重复单词的最后一个实例,并删除所有其余的。对于那些只关心重复而不关心单词顺序的人来说,这应该是可行的!
#9
-1
var string = "spanner, span, spaniel, span";
var strArray= string.split(",");
var unique = [];
for(var i =0; i< strArray.length; i++)
{
eval(unique[strArray] = new Object());
}
//You can easily traverse the unique through foreach.
//你可以很容易地遍历唯一的foreach。
I like this for three reason. First, it works with IE8 or any other browser.
我喜欢这个有三个原因。首先,它与IE8或其他浏览器兼容。
Second. it is more optimized and guaranteed to have unique result.
第二。它更加优化,并且保证有独特的结果。
Last, It works for Other String array which has White space in their inputs like
最后,它适用于其他在输入中有空格的字符串数组
var string[] = {"New York", "New Jersey", "South Hampsire","New York"};
for the above case there will be only three elements in the string[] which would be uniquely stored.
对于上述情况,字符串[]中只有三个元素是惟一存储的。