麻豆黑色丝袜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]

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

主站蜘蛛池模板: 最近的中文字幕视频完整| 一区精品麻豆入口| 欧美黑人xxxx| 亚洲综合无码一区二区| 狠狠色噜噜狠狠狠狠色吗综合| 秋霞日韩一区二区三区在线观看| 成全高清视频免费观看| 日本不卡中文字幕| 干妞网在线观看| 在线看福利影| 天天5g影院永久免费地址| 伊人久久大香线蕉久久婷婷| 9999av| 东北小彬系列chinese| 最近中文字幕免费4| 男人j进女人p免费视频不要下载的| 美团外卖猛男男同38分钟 | 欧美人与动人物乱大交| 在线|一区二区三区四区| 国产极品视觉盛宴| 岛国片在线播放| 国产一区二区在线观看app| 久久精品a亚洲国产v高清不卡| 国产精品宾馆在线| 99国产成+人+综合+亚洲欧美| 波多野结衣新婚被邻居| 久久综合九色欧美综合狠狠| 男人的肌肌捅女人的肌肌| 男人把女人桶爽30分钟应用| 午夜网站在线观看| 国产福利精品一区二区| 在线看无码的免费网站| 精品国产福利一区二区| 久久99精品国产自在现线小黄鸭| 国产影片中文字幕| 亚洲中字慕日产2020| 日本大片免费一级| 国产激情电影综合在线看 | 国产免费午夜| 久久人人爽人人爽人人片av不| 东北女大战28公分黑人|