remove duplicate strings that are case insensitive
How can i make this script case insensitve (e.g. at the moment i get 'damon' and 'Damon'. Say i just want to keep the lowercase 'damon'?)?
var unique = function(origArr) {
var newArr = [],
origLen = origArr.length,
found,
x, y;
for ( x = 0; x < origLen; x++ ) {
found = undefined;
for ( y = 0; y < newArr.length; y++ ) {
if ( origArr[x] === newArr[y] ) {
found = true;
break;
}
}
if ( !found) newArr.push( origArr[x] );
}
return newArr;
}
var a = ['jeffrey','allie','patty','damon','Damon','zach','Jeffrey','allie','patty','damon','zach','joe'];
alert(a.join(',').toLowerCase().split(',').sort().join(',').replace(/(([^,]+),)(?=\2)/g,'').split(','));
Thank you both these are great and a big help.
With yours Julien I get the result: 'allie, damon, jeffrey, joe, patty, zach,jeffrey' (for some reason i get an extra 'jeffery')?
As an alternative how would i modify the script to have leading caps (Allie, Damon, Jeffrey, Joe, Patty, Zach)?
I do not understand your extra jeffrey. A remove spaces of the string with a replace (/\s/g,'') could be useful ?
Since it's easy to capitalize the first letters of sentences with a new method or directly
Code:
// A new method
String.prototype.ucFirst=function(){return this.replace(/\b(\w)/g,function(){return arguments[1].toUpperCase()})}
alert("james o'connor".ucFirst());
var a = ['jeffrey','allie','patty','damon','Damon','zach','Jeffrey','allie','patty','damon','zach','joe'];
var b=a.join(',').toLowerCase().split(',').sort().join(',').replace(/(([^,]+),)(?=\2)/g,'').replace(/\b(\w)/g,function(){return arguments[1].toUpperCase();}).split(',');
alert(b);
EDIT : Your string ('allie, damon, jeffrey, joe, patty, zach,jeffrey') is made with spaces before damon, jeffrey... zach, and not before the first allie and the last jeffrey ! Then use «simply» a
Code:
var b=a.join(',').toLowerCase().replace(/\s/g,'').split(',').sort().join(',').replace(/(([^,]+),)(?=\2)/g,'').replace(/\b(\w)/g,function(){return arguments[1].toUpperCase();}).split(',');
Last edited by 007Julien; 01-04-2013 at 06:48 AM.
Reason: complements
Bookmarks