<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ??一站式輕松地調用各大LLM模型接口,支持GPT4、智譜、豆包、星火、月之暗面及文生圖、文生視頻 廣告
                # JS實現數組去重方法 ## 方法一 >[info]【**原理**】: 雙層循環,外層循環元素,內層循環時比較值 如果有相同的值則跳過,不相同則push進數組 ~~~ function unique(arr){ var res = []; for(var i=0;i<arr.length;i++){ for(var j=i+1;j<arr.length;j++){ if(arr[i] === arr[j]){ j = ++i; } } res.push(arr[i]); } return res; } var arr1 = [1,2,3,5,4,6,1,2,4,7]; console.log(unique(arr1));//(7) [3, 5, 6, 1, 2, 4, 7] ~~~ ## 方法二 >[info]【**原理**】: 利用splice直接在原數組進行操作 雙層循環,外層循環元素,內層循環時比較值 值相同時,則刪去這個值 ~~~ function unique2(arr){ for(i = 0; i < arr.length; i++){ for(j = i + 1; j < arr.length; j++){ if(arr[i] == arr[j]){ arr.splice(j,1); j--; } } } return arr; } var arr1 = [1,2,3,5,4,6,1,2,4,7]; console.log(unique2(arr1));//(7) [1, 2, 3, 5, 4, 6, 7] ~~~ ## 方法三 >[info]【**原理**】: 運用遞歸的思想 先排序,然后從最后開始比較,遇到相同,則刪除 ~~~ function unique3(arr){ arr.sort(function(a,b){//對數組進行排序才能方便比較 return a-b; }); function loop(index){ if(index >=1){ if(arr[index] === arr[index-1]){ arr.splice(index,1); } loop(index-1); } } loop(arr.length-1); return arr; } var arr1 = [1,2,3,5,4,6,1,2,4,7]; console.log(unique3(arr1));//(7) [1, 2, 3, 4, 5, 6, 7] ~~~ ## 方法四 >[info]【**原理**】: >利用indexOf以及forEach ~~~ function unique4(arr){ var res = []; arr.forEach(function(v, i ,arr){ var bool = arr.indexOf(v,i+1); if(bool === -1){ res.push(v); } }) return res; } var arr1 = [1,2,3,5,4,6,1,2,4,7]; console.log(unique4(arr1));//(7) [3, 5, 6, 1, 2, 4, 7] ~~~ ## 方法五 >[info]【**原理**】: >利用ES6的set ~~~ function unique5(arr){ return Array.from(new Set(arr)); } var arr1 = [1,2,3,5,4,6,1,2,4,7]; console.log(unique5(arr1));//(7) [1, 2, 3, 5, 4, 6, 7] ~~~
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看