首页 > 代码库 > 数组去重

数组去重

<!doctype html><html lang="en"><head>  <meta charset="UTF-8">  <title>Document</title></head><body>  <script>  //为Array类型增加函数  使其可以去重  Array.prototype.eraseDuplicate = function() {    var after = {};    var re = [];    for (var i = 0; i < this.length; i++) {      if (!after[this[i]]) {        after[this[i]] = 1;        re.push(this[i]);      }    }    return re;  };  console.log([1, 2, 3, 3, 4, 55, 6, 6].eraseDuplicate());  //写一个可以对二位数组的  找出出现过的数字(不重复)  var arr = [2, 3, [4, 6, [3, 8]], 12, 10];  Array.prototype.getUnq = function() {    var rs = [];    var arr = this;    (function getUnique(arr) {      for (var i = 0, len = arr.length; i < len; i++) {        if (arr[i].constructor == Array) {          arguments.callee(arr[i]);        } else {          if (rs.indexOf(arr[i]) == -1) {            rs.push(arr[i]);          }        }      }    })(arr);    return rs;  }  rs = arr.getUnq();  console.log(rs);  //PS  递归函数应始终使用arguments.callee  </script></body></html>

 

数组去重