首页 > 代码库 > 排除数组重复--javascript

排除数组重复--javascript

<!DOCTYPE html>
<html>
<head lang="en">
    <meta charset="UTF-8">
    <title>排除数组重复</title>
</head>
<body>
<script>
    Array.prototype.checkRepeat=function(){
        var checkR=[];
        var len=this.length;
//        for(var i=0;i<len;i++){
//            for(var j=-1;j<checkR.length;j++){
//                if(this[i]==this[j]){
//                    break;
//                }else if(j==checkR.length-1){
//                    checkR.push(this[i]);
//                }
//            }
//
//        }
        for(var i=0;i<len;i++){
            for(var j=i+1;j<len;j++){
                if(this[i]==this[j]){
                    this.splice(j,1);//splice删除某个片段
                }
            }
        }

    }
    var show=[0,2,5,6,4,0,4,6,2,3]
    show.checkRepeat();
    console.log(show)
</script>

</body>
</html>

排除数组重复--javascript