1.
var arr=str.split(""); for(var i=0;arr.length-1>i;i++){ for(var j=i+1;j<arr.length;j++){ if(arr[j]==arr[i]){ arr.splice(j,1); //删除之后,数组长度随之减少 j--; } } } //alert(arr.toString()); //生成的字符串由逗号隔开 //arr.join(""); //生成的字符串无分割符 //arr.join("-"); //join方法可指定生成的字符串的分隔符
2.
Array.prototype.unique = function() { var data = []; var a = {}; //声明一个对象,javascript的对象可以当哈希表用 for (var i = 0; i < this.length; i++) { if(!a[this[i]]){ a[this[i]] = true; data[data.length] = this[i]; } } return data; }; var a = new Array("5","7","7"); alert(a); alert(a.unique());
3.
Array.prototype.del = function(){ var a = {}, c = [], l = this.length; for (var i = 0; i < l; i++) { var b = this[i]; var d = (typeof b) + b; if (a[d] === undefined) { c.push(b); a[d] = 1; } } return c; } var arr = [1,2,5,2,7,3,8]; alert(arr.del());