<td id="iz0mx"><em id="iz0mx"></em></td>

    1. <progress id="iz0mx"></progress>
      <p id="iz0mx"></p>
    2. <progress id="iz0mx"><menu id="iz0mx"></menu></progress>
      <code id="iz0mx"></code>

      千鋒教育-做有情懷、有良心、有品質的職業教育機構

      手機站
      千鋒教育

      千鋒學習站 | 隨時隨地免費學

      千鋒教育

      掃一掃進入千鋒手機站

      領取全套視頻
      千鋒教育

      關注千鋒學習站小程序
      隨時隨地免費學習課程

      當前位置:首頁  >  技術干貨  > js數組中去除重復值的幾種方法

      js數組中去除重復值的幾種方法

      來源:千鋒教育
      發布人:lcy
      時間: 2023-07-03 17:25:00 1688376300

        在 JavaScript 中,有幾種常見的方法可以從數組中去除重復值:

        1. 使用 Set:

      var arr = [1, 2, 3, 3, 4, 5, 5];
      var uniqueArr = [...new Set(arr)];
      console.log(uniqueArr); // 輸出: [1, 2, 3, 4, 5]

         2. 使用 filter 方法和 indexOf:

      var arr = [1, 2, 3, 3, 4, 5, 5];
      var uniqueArr = arr.filter((value, index, self) => {
      return self.indexOf(value) === index;
      });
      console.log(uniqueArr); // 輸出: [1, 2, 3, 4, 5]

         3. 使用 reduce 方法:

      var arr = [1, 2, 3, 3, 4, 5, 5];
      var uniqueArr = arr.reduce((accumulator, currentValue) => {
      if (!accumulator.includes(currentValue)) {
      accumulator.push(currentValue);
      }
      return accumulator;
      }, []);
      console.log(uniqueArr); // 輸出: [1, 2, 3, 4, 5]

         4. 使用 ES6 的 includes 方法:

      var arr = [1, 2, 3, 3, 4, 5, 5];
      var uniqueArr = arr.filter((value, index, self) => {
      return self.indexOf(value) === index;
      });
      console.log(uniqueArr); // 輸出: [1, 2, 3, 4, 5]

         以上是幾種常見的方法用于從 JavaScript 數組中去除重復值。每種方法都有其優勢和適用場景,選擇適合你的情況的方法即可。

      js數組中去除重復值的幾種方法

      聲明:本站稿件版權均屬千鋒教育所有,未經許可不得擅自轉載。
      10年以上業內強師集結,手把手帶你蛻變精英
      請您保持通訊暢通,專屬學習老師24小時內將與您1V1溝通
      免費領取
      今日已有369人領取成功
      劉同學 138****2860 剛剛成功領取
      王同學 131****2015 剛剛成功領取
      張同學 133****4652 剛剛成功領取
      李同學 135****8607 剛剛成功領取
      楊同學 132****5667 剛剛成功領取
      岳同學 134****6652 剛剛成功領取
      梁同學 157****2950 剛剛成功領取
      劉同學 189****1015 剛剛成功領取
      張同學 155****4678 剛剛成功領取
      鄒同學 139****2907 剛剛成功領取
      董同學 138****2867 剛剛成功領取
      周同學 136****3602 剛剛成功領取
      相關推薦HOT
      亚洲国产欧美一区,二区 亚洲国产日产无码精品一 亚洲精品国产第一综合色吧 国产伦精品一区二区三视频

      <td id="iz0mx"><em id="iz0mx"></em></td>

      1. <progress id="iz0mx"></progress>
        <p id="iz0mx"></p>
      2. <progress id="iz0mx"><menu id="iz0mx"></menu></progress>
        <code id="iz0mx"></code>