利用json去重 原型--实现继承

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8" content="text/html" name="01">
    <title>利用json去重  原型--实现继承</title>
</head>
<body>
<script>
//利用json去重  原型--实现继承
   var arr = [1,2,4,3,45,6,33,4,2,6,"a"];
    Array.prototype.quchong=function(){
        var newArr=[];
        var json={};
        for(var i=0;i<this.length;i++){
            if(!json[this[i]]){
                newArr.push(this[i]);
                json[this[i]]=true;
            }
        }
        return newArr;
    };
  var unq  = arr.quchong();
   document.write(unq);
document.write('<br>');
       //排序
  unq.sort(function compare(a,b){
    return a-b;  //倒序 b-a
});
document.write(unq);//利用splice 索引值删除字母
document.write('<br>');
unq.splice(7);
document.write(unq);
</script>
</body>
</html>

猜你喜欢

转载自blog.csdn.net/qq_40143330/article/details/79659300
今日推荐