前言
一般的方法此處也不列舉了,還是有很多的,如雙層循環判斷是否相等,或新建數組比較再push等等,需要注意的是,使用splice方法移除元素時,有可能會導致數組塌陷問題,需要處理一下
本文中介紹了多種數組去重的方法,使用了較多的高階方法及API,并給出相應解釋及語法,還有其他多種組合調用方式,原理邏輯其實都差不多,其中for循環可以與forEach方法相互轉換,因此此處便不再一一列舉,大家如果有更好的算法,可以留言給我,虛心請教!!
給定一個數組 [1,2,2,4,null,null,'3','abc',3,5,4,1,2,2,4,null,null,'3','abc',3,5,4] 去除重復項
1
|
let arr = [1,2,2,4, null , null , '3' , 'abc' ,3,5,4,1,2,2,4, null , null , '3' , 'abc' ,3,5,4] |
1. 利用對象的 key 唯一
眾所周知,對象的key不可重復,否則后者將覆蓋前者。利用該特性,實現數組去重,遍歷數組,將數組的每一項做為對象的key值
1
2
3
4
5
6
7
8
9
10
11
12
|
let obj = {}; for (let i = 0; i < arr.length; i++) { let item = arr[i] if (obj[item] !== undefined) { arr.splice(i, 1); i--; // 解決刪除元素后,數組塌陷問題 continue ; } obj[item] = item } // arr: [1, 2, 4, null, "3", "abc", 3, 5] |
2. 交換元素位置從而替換調 splice方法
上述方法存在一定的性能問題,也就是說,基于splice實現刪除性能不太好,當前項被刪除后,隨后每一項的索引都要向前移動一位,數據量較龐大時,一定會影響性能。
基于以上考慮,交換元素的位置,效率會更高一點,若當前元素重復,則與數組最后一位元素交換位置,i--再次進行判斷即可,同時length--,操作數組的長度實現刪除數組的最后一個元素,這樣便不會影響到數組中其他元素
1
2
3
4
5
6
7
8
9
10
11
12
|
let obj = {}; for (let i = 0; i < arr.length; i++) { let item = arr[i] if (obj[item] !== undefined) { arr[i] = arr[arr.length-1] arr.length--; i--; continue ; } obj[item] = item } // arr: [1, 2, 4, null, "3", "abc", 3, 5] |
3. Array.filter + Array.indexOf
filter() 方法:創建一個新數組,新數組中的元素是指定數組中符合某種條件的所有元素。如果沒有符合條件的元素則返回空數組。
語法:array.filter(function(item,index,arr))
- filter() 不會對空數組進行檢測。
- filter() 不會改變原始數組。
原理:返回 item 第一次出現的位置等于當前的index的元素
1
2
3
|
let newArr = arr.filter((item, index) => arr.indexOf(item) === index); // [1, 2, 4, null, "3", "abc", 3, 5] |
4. Array.filter + Object.hasOwnProperty
hasOwnProperty() 方法:返回一個布爾值,表示對象自身屬性中是否具有指定的屬性
原理:利用對象的鍵名不可重復的特點
1
2
3
|
let obj = {} arr.filter(item => obj.hasOwnProperty( typeof item + item) ? false : (obj[ typeof item + item] = true )) |
5. Array.reduce + Array.includes
reduce() 方法:接收一個函數作為累加器,數組中的每個值從左到右開始計算,最終計算為一個值。
語法:arr.reduce(function(total, currValue, currIndex, arr), initValue)
reduce() 對于空數組是不會執行回調函數的。
total:必需。初始值, 或者計算結束后的返回值
currValue:必需。當前元素
currIndex:可選。當前元素的索引
arr :可選。當前數組對象。
initValue:可選。累加器初始值
一個空數組調用reduce()方法且沒有提供初始值,會報錯。
一個空數組調用reduce()方法且提供了初始值,將直接返回該初始值,不會調用 callback 函數。
非空數組調用reduce()提供初始值,則total將會等于初始值,且 currValue從第一個元素開始;若沒有提供初始值,則 total 會等于的第一個元素值,且 currValue將會從第二個元素開始。
1
2
3
4
5
6
7
|
let newArr = arr.reduce((accu, cur) => { return accu.includes(cur) ? accu : accu.concat(cur); // 1. 拼接方法 // return accu.includes(cur) ? accu : [...accu, cur]; // 2. 擴展運算 }, []); // [1, 2, 4, null, "3", "abc", 3, 5] |
6. Array.indexOf
indexOf() 方法:返回數組中某個指定的元素位置。該方法遍歷數組,查找有無對應元素并返回元素第一次出現的索引,未找到指定元素則返回 -1。
1
2
3
4
5
6
7
8
9
|
let newArr = [] for ( var i = 0; i < arr.length; i++) { if (newArr.indexOf(arr[i]) === -1) newArr.push(arr[i]) } //等同于 forEach 寫法 arr.forEach( item => newArr.indexOf(item) === -1 ? newArr.push(item) : '' ) console.log(newArr) // [1, 2, 4, null, "3", "abc", 3, 5] |
7. Array.includes
includes() 方法:用來判斷一個數組是否包含一個指定的值,如果是返回 true,否則false。
1
2
3
4
5
6
7
8
9
|
let newArr = [] for ( var i = 0; i < arr.length; i++) { if (!newArr.includes(arr[i])) newArr.push(arr[i]) } //等同于 forEach 寫法 arr.forEach( item => !newArr.includes(item) ? newArr.push(item) : '' ) console.log(newArr) // [1, 2, 4, null, "3", "abc", 3, 5] |
8. new Set + 擴展運算符 || Array.from
ES6 提供了新的數據結構 Set。類似于數組,但是成員的值都是唯一的,沒有重復的值。
Set本身是一個構造函數,可以接受一個具有 iterable 接口數據結構作為參數(如數組,字符串),用來初始化。
1
2
3
4
5
|
let newArr = [... new Set(arr)]; // [1, 2, 4, null, "3", "abc", 3, 5] let newArr = Array.from( new Set(arr)); // [1, 2, 4, null, "3", "abc", 3, 5] let newStr = [... new Set( 'ababbc' )].join( '' ) // 'abc' |
9. new Map
ES6 提供了新的數據結構 Map。類似于對象,也是鍵值對的集合,但是“鍵”的范圍不限于字符串,各種類型的值(包括對象)都可以當作鍵。
set方法設置鍵名key對應的鍵值為value,然后返回整個 Map 結構。如果key已經有值,則鍵值會被更新,否則就新生成該鍵。
get方法讀取key對應的鍵值,如果找不到key,返回undefined。
has方法返回一個布爾值,表示某個鍵是否在當前 Map 對象之中。
1
2
3
4
5
6
7
8
9
10
|
let map = new Map(); let newStr = []; for (let i = 0; i < arr.length; i++) { if (!map.has(arr[i])) { map.set(arr[i], true ); newStr.push(arr[i]); } } console.log(newArr) // [1, 2, 4, null, "3", "abc", 3, 5] |
總結
到此這篇關于JS數組去重的九種高階方法的文章就介紹到這了,更多相關JS數組去重內容請搜索服務器之家以前的文章或繼續瀏覽下面的相關文章希望大家以后多多支持服務器之家!
原文鏈接:https://www.cnblogs.com/echoyya/p/14555831.html