麻豆黑色丝袜jk制服福利网站-麻豆精品传媒视频观看-麻豆精品传媒一二三区在线视频-麻豆精选传媒4区2021-在线视频99-在线视频a

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

手機站
千鋒教育

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

千鋒教育

掃一掃進入千鋒手機站

領取全套視頻
千鋒教育

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

當前位置:首頁  >  千鋒問問  > js去重的方法有哪些

js去重的方法有哪些

js去重 匿名提問者 2023-08-03 20:00:21

js去重的方法有哪些

我要提問

推薦答案

  在JavaScript中,數組去重有多種方法。以下是三種常用的去重方法:

千鋒教育

  1. 使用Set數據結構:

  Set是一種ES6中新增的數據結構,它可以存儲唯一的值,因此可以用來實現數組去重。

function removeDuplicatesWithSet(arr) {
const uniqueArray = [...new Set(arr)];
return uniqueArray;
}

const originalArray = [1, 2, 2, 3, 4, 4, 5];
const uniqueArray = removeDuplicatesWithSet(originalArray);
console.log(uniqueArray); // 輸出: [1, 2, 3, 4, 5]

 

  2. 使用Array.filter()方法:

  `Array.filter()`方法可以用于過濾數組中的元素,我們可以結合`indexOf()`方法來篩選出數組中第一次出現的元素,從而實現去重。

function removeDuplicatesWithFilter(arr) {
return arr.filter((value, index, self) => self.indexOf(value) === index);
}

const originalArray = [1, 2, 2, 3, 4, 4, 5];
const uniqueArray = removeDuplicatesWithFilter(originalArray);
console.log(uniqueArray); // 輸出: [1, 2, 3, 4, 5]

 

  3. 使用Object鍵值對:

  通過將數組元素作為對象的鍵名,利用對象鍵名的唯一性實現數組去重。

function removeDuplicatesWithObject(arr) {
const obj = {};
arr.forEach(item => obj[item] = true);
return Object.keys(obj).map(Number);
}

const originalArray = [1, 2, 2, 3, 4, 4, 5];
const uniqueArray = removeDuplicatesWithObject(originalArray);
console.log(uniqueArray); // 輸出: [1, 2, 3, 4, 5]

 

  以上三種方法都可以實現數組去重,你可以根據項目需求和個人喜好選擇最合適的方法。

其他答案

  •   在JavaScript中,有多種方法可以實現數組去重。以下是三種常用的去重方法:

      1. 使用Set數據結構:

      Set是一種ES6中新增的數據結構,它可以存儲唯一的值,因此可以用來實現數組去重。

      function removeDuplicatesWithSet(arr) {

      const uniqueArray = Array.from(new Set(arr));

      return uniqueArray;

      }

      const originalArray = [1, 2, 2, 3, 4, 4, 5];

      const uniqueArray = removeDuplicatesWithSet(originalArray);

      console.log(uniqueArray); // 輸出: [1, 2, 3, 4, 5]

      2. 使用Array.reduce()方法:

      `Array.reduce()`方法可以用來迭代數組,并將結果累積到一個值中。我們可以利用它來實現數組去重。

      function removeDuplicatesWithReduce(arr) {

      return arr.reduce((acc, current) => {

      if (!acc.includes(current)) {

      acc.push(current);

      }

      return acc;

      }, []);

      }

      const originalArray = [1, 2, 2, 3, 4, 4, 5];

      const uniqueArray = removeDuplicatesWithReduce(originalArray);

      console.log(uniqueArray); // 輸出: [1, 2, 3, 4, 5]

      3. 使用for循環和indexOf()方法:

      通過遍歷數組,利用`indexOf()`方法判斷元素是否在新數組中已存在,從而實現數組去重。

      function removeDuplicatesWithForLoop(arr) {

      const uniqueArray = [];

      for (let i = 0; i < arr.length; i++) {

      if (uniqueArray.indexOf(arr[i]) === -1) {

      uniqueArray.push(arr[i]);

      }

      }

      return uniqueArray;

      }

      const originalArray = [1, 2, 2, 3, 4, 4, 5];

      const uniqueArray = removeDuplicatesWithForLoop(originalArray);

      console.log(uniqueArray); // 輸出: [1, 2, 3, 4, 5]

      以上三種方法都能有效地實現數組去重,你可以根據具體場景和數組規模選擇最適合的方法。

  •   在JavaScript中,數組去重可以通過多種方法實現。以下是三種常用的去重方法:

      1. 使用Set數據結構:

      Set是一種ES6中引入的數據結構,它可以存儲唯一的值,因此可以用來實現數組去重。

      function removeDuplicatesWithSet(arr) {

      const uniqueArray = Array.from(new Set(arr));

      return uniqueArray;

      }

      const originalArray = [1, 2, 2, 3, 4, 4, 5];

      const uniqueArray = removeDuplicatesWithSet(originalArray);

      console.log(uniqueArray); // 輸出: [1, 2, 3, 4, 5]

      2. 使用Array.indexOf()方法:

      通過遍歷數組并利用`indexOf()`方法來判斷元素是否在新數組中已存在,從而實現數組去重。

      function removeDuplicatesWithIndexOf(arr) {

      const uniqueArray = [];

      for (let i = 0; i < arr.length; i++) {

      if (uniqueArray.indexOf(arr[i]) === -1) {

      uniqueArray.push(arr[i]);

      }

      }

      return uniqueArray;

      }

      const originalArray = [1, 2, 2, 3, 4, 4, 5];

      const uniqueArray = removeDuplicatesWithIndexOf(originalArray);

      console.log(uniqueArray); // 輸出: [1, 2, 3, 4, 5]

      3. 使用Array.filter()方法:

      `Array.filter()`方法可以用來過濾數組中的元素,我們可以結合`indexOf()`方法來篩選出數組中第一次出現的元素,從而實現去重。

      function removeDuplicatesWithFilter(arr) {

      return arr.filter((value, index, self) => self.indexOf(value) === index);

      }

      const originalArray = [1, 2, 2, 3, 4, 4, 5];

      const uniqueArray = removeDuplicatesWithFilter(originalArray);

      console.log(uniqueArray); // 輸出: [1, 2, 3, 4, 5]

      以上三種方法都能有效地實現數組去重,你可以根據具體情況選擇最適合的方法。

主站蜘蛛池模板: 日韩免费三级电影| 97久久久亚洲综合久久88| 被夫上司强迫的女人在线| 激情综合色综合久久综合| 日本公与熄乱理在线播放370| 天天操夜| 美国式禁忌5太大了| 嫩草影院在线入口| 妖精视频网址| 日本漂亮继坶中文字幕| 久久天天躁日日躁狠狠躁| 久久久不卡国产精品一区二区| 亚洲性色高清完整版在线观看| 女偶像私下的y荡生活| 中文字幕avdvd| 欧美成人精品第一区| 最近高清中文字幕在线国语5| 久久九九国产精品怡红院| 欧美色欧美亚洲高清在线视频| 欧美精品久久天天躁| 日本在线高清版卡免v| 奇米视频7777| 波多野结无码高清中文| 欧洲美女与动性zozozo| 看三级黄色片| 精品国产成a人在线观看| 久久99精品国产麻豆不卡| 韩国v欧美v亚洲v日本v| 日本一区中文字幕日本一二三区视频| 大胸校花被老头粗暴在线观看 | 在线网站你懂得| 亚洲精品自拍视频| 国产精品9999久久久久仙踪林| 打开腿我想亲亲你下面视频| 精品毛片视频| 秋葵视频在线观看在线下载| 久久一日本道色综合久久m| 欧美国产综合| 波多野结衣同性| 百合潮湿的欲望| 好猛好能干h|