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/
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...
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);
For non supporting browsers you can tackle it by adding this in your 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;
};
}
In getUniqueWordString function, we are filtering redundant words and then joining back with delimiter. Added one case also if in Input string words exist in Upper and lower case both.
function getUniqueWordString(str, delimiter) {
return str.toLowerCase().split(delimiter).filter(function(e, i, arr) {
return arr.indexOf(e, i+1) === -1;
}).join(delimiter);
}
let str = "spanner, span, spaniel, span, SPAN, SpaNiel";
console.log(getUniqueWordString(str, ", "))
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 :)
// 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);
Live DEMO
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With