数组去重js

2018-01-23  本文已影响0人  Delicia_Lani
         <!--方式一:索引方式-->
    <script>
        var arry = [1,2,4,6,78,4,2,1,3,7];
        var newArry = [];
        function aa(){
            for(var i=0;i<arry.length;i++){
                if(newArry.indexOf(arry[i])==-1){
                    newArry.push(arry[i]);
                }
            }
            return newArry;
        }
        aa();
        console.log(newArry);
    </script>
    <!--方式二:排序方式-->
    <script>
        var arry = [1,2,4,6,78,4,2,1,3,7];
        var newArry = arry.sort();
        var res=[];
        function aa(){
            for(var i=0;i<newArry.length;i++){
            if(newArry[i]!==res[res.length-1]){
                res.push(newArry[i]);
                }
            }
            return res;
        }           
        aa()
        console.log(res);
    </script>
上一篇下一篇

猜你喜欢

热点阅读