返回数组中不重复的元素

这是实现结构伪类only-of-type的部分代码:


    var ret= ["span","span","strong","span","b"]
    var norepeat = function(array){
      var set = array.join(",")+",";
      while(array.length){
        var el = array.shift();
        set = set.replace(el+",","");
        if(set.indexOf(el+",") != -1){
          set = set.replace(new RegExp(el+",","g"),"");
          array = set.replace(/,$/g,"").split(",");
        }else{
          set = el+","+set;
        }
      }
      return set.replace(/,$/g,"").split(",")
    }
    alert(norepeat(ret))//最后返回b,strong
var ret= ["span","span","strong","span","b"] var norepeat = function(array){ var set = array.join(",")+","; while(array.length){ var el = array.shift(); set = set.replace(el+",",""); if(set.indexOf(el+",") != -1){ set = set.replace(new RegExp(el+",","g"),""); array = set.replace(/,$/g,"").split(","); }else{ set = el+","+set; } } return set.replace(/,$/g,"").split(",") } alert(norepeat(ret))//最后返回b,strong

运行代码

觉得效果不高,向无忧高手求救。下面是客服果果的(好强大的正则替换啊):


var ret= ["span","span","strong","span","b"];
var norepeat=function (x) {
  var y='c0cb5f0fcf239ab3d9c1fcd31fff1efc';
  return x.sort().join(y).replace(RegExp('(.*('+y+'|$))\\1+','g'),'').split(y);
};
alert(norepeat(ret));
var ret= ["span","span","strong","span","b"]; var norepeat=function (x) { var y='c0cb5f0fcf239ab3d9c1fcd31fff1efc'; return x.sort().join(y).replace(RegExp('(.*('+y+'|$))\\1+','g'),'').split(y); }; alert(norepeat(ret));

运行代码

abcdreamer 的实现(此实现能过滤非纯字符串数组里的重复,我比较喜欢):


var a = ["span","span","strong","span","b"];
function norepeat(a) {
  var i = a.length, j, b;
  while (i--) {
    j = i;
    b = false;
    while (j--) {
      if (a[i] === a[j]) {
        a.splice(j, 1);
        i--;
        b = true;
      }
    }
    if (b) a.splice(i, 1);
  }
  return a;
}
alert(norepeat(a));
var a = ["span","span","strong","span","b"]; function norepeat(a) { var i = a.length, j, b; while (i--) { j = i; b = false; while (j--) { if (a[i] === a[j]) { a.splice(j, 1); i--; b = true; } } if (b) a.splice(i, 1); } return a; } alert(norepeat(a));

运行代码

增加campaign 提供的解决方案!


	var ret= [{a:1},{a:1},"strong","span","span","b"],r=[];
	 for(var i=0,l=ret.length,tmp={};i<l;i++){
		tmp[ret[i]]?tmp[ret[i]]++:(tmp[ret[i]]=1);
	 }
	 for(var p in tmp){
		if(tmp[p]==1)
			r.push(p);
	 }
	 alert(r)
 

哪位高手有更好的实现,请告诉我一声!


看来有些人不明白norepeat函数与unique函数的区别,norepeat是去除数组中重复的元素,如果数组中有两个a,则去除这两个,unique则会去除其中之一。下面是unique函数的实现:


      Array.prototype.inArray = function(el){
        for (var i=0,n=this.length;i<n;i++ )
          if(this[i]===el)return true;
        return false;
      }
      Array.prototype.unique = function() {
        var i = 0, n = this.length,ret = [];
        for (; i < n; i++)
          if (!ret.inArray(this[i])) ret.push(this[i]);
        return ret;
      };
      var ret= ["span","span","strong","span","b"]
      alert(ret.unique())  //返回"span","strong","b"
<!doctype html> <html dir="ltr" lang="zh-CN"> <head> <meta charset="utf-8"/> <meta http-equiv="X-UA-Compatible" content="IE=7"> <title>unique函数 By 司徒正美</title> <script> Array.prototype.inArray = function(el){ for (var i=0,n=this.length;i<n;i++ ) if(this[i]===el)return true; return false; } Array.prototype.unique = function() {//取数组中唯一的元素并放进新数组中返回 var i = 0, n = this.length,ret = []; for (; i < n; i++) if (!ret.inArray(this[i])) ret.push(this[i]); return ret; }; var ret= ["span","span","strong","span","b"] alert(ret.unique()) </script> </head> <body> </body> </html>

运行代码

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值