# 彻底搞定各种数组去重需求

## 一、简单的数组去重

### 1、new Set

 const arr = [1,2,3,1,2,2]
new Set(arr)
set 结构转换成 数组 有两种方式 [...new Set(arr)]和 Array.from(new Set(arr))
//  [1, 2, 3]


### 2、使用 遍历(map、for循环、forEach) + indexOf

const arr = [1,2,3,1,2,2]
let arr2 = []
for(let i=0;i<arr.length;i++){
if(arr2.indexOf(arr[i]) === -1){
arr2.push(arr[i])
}
}
//=> arr2 [1, 2, 3]


### 3、使用 reduce、reduceRight 去重

const arr = [1,2,3,1,2,2]
arr.reduce((result, cur, index, array)=>{
if(result.indexOf(cur) === -1){
result.push(cur)
}
return result
},[])


### 4、利用对象 key的唯一性 去重

const obj = {}
const result = []
const arr = [1,2,3,1,2,2]
arr.map(el=>{
if(!obj[el]){
result.push(el)
obj[el] = el
}
})
console.log(result) //=> [1, 2, 3]


const arr = [1,2,3,1,2,2]
const obj = {}
arr.reduce((result, cur, index,array)=>{
if(!obj[cur]){
result.push(cur)
obj[cur] = cur
}
return result
},[])
//=> [1, 2, 3]


### 5、filter 去重

const arr = [1,2,3,1,2,2]
arr.filter((el,index)=> !arr.slice(0,index).some(t=> t === el))

arr.filter((el,index)=> arr.indexOf(el)
//=> [1, 2, 3]


## 二、复杂的JSON数组去重

const arr = [{id:1, name: 'liming'},{id:2, name:'xiaohong'}, {id:1, name: 'liming2'}]

### 1、使用 遍历(map、for循环、forEach) + indexOf

let arr = [{id:1, name: 'liming'},{id:2, name:'xiaohong'}, {id:1, name: 'liming2'}]
let arr2 = []
let result = []
for(let i=0;i<arr.length;i++){
if(arr2.indexOf(arr[i].id) === -1){
result.push(arr[i])
arr2.push(arr[i].id)
}
}
// result => [{id: 1, name: 'liming'},{id: 2, name:'xiaohong}]
// arr2 => [1,2]


### 2、使用 reduce、reduceRight 去重

let arr = [{id:1, name: 'liming'},{id:2, name:'xiaohong'}, {id:1, name: 'liming2'}]
arr.reduce((result, cur, index, array)=>{
let r = array.slice(0,index).filter(el=> el.id === cur.id)
if(!r.length){
result.push(cur)
}
return result
},[])
// result => [{id: 1, name: 'liming'},{id: 2, name:'xiaohong}]
// arr2 => [1,2]


### 3、利用对象 key的唯一性 去重

let arr = [{id:1, name: 'liming'},{id:2, name:'xiaohong'}, {id:1, name: 'liming2'}]
let obj = {}
let result = []
arr.map((cur,index)=>{
f(!obj[cur.id]){
result.push(cur)
obj[cur.id] = true
}
})
// result => [{id: 1, name: 'liming'},{id: 2, name:'xiaohong}]


### 4、利用 map + filter

let arr = [{id:1, name: 'liming'},{id:2, name:'xiaohong'}, {id:1, name: 'liming2'}]
let result = []
arr.map((cur, index)=>{
let r = arr.slice(0,index).filter(el=> el.id === cur.id)
if(!r.length){
result.push(cur)
}
return result
})
// result => [{id: 1, name: 'liming'},{id: 2, name:'xiaohong}]


## 三、其他数组去重的需求

### 1、复杂去重 加 合并重复项的某些值

objArray = [
{ id: 1, name: "A", number: 1 },
{ id: 2, name: "B", number: 2 },
{ id: 1, name: "B", number: 3 },
{ id: 1, name: "C", number: 4 },
{ id: 1, name: "D", number: 5 },
];
let newArr = objArray.reduceRight((cur, item, index, array) => {
let curArr = cur.filter((c) => c.id === item.id);
if (curArr.length > 0) {
let nameObj = cur.find((oo) => oo.id === item.id);
nameObj.name = Array.isArray(nameObj.name)
? [...nameObj.name, item.name]
: [nameObj.name, item.name];
nameObj.number = Array.isArray(nameObj.number)
? [...nameObj.number, item.number]
: [nameObj.number, item.number];
} else {
cur.push(item);
}
return cur;
}, []);
console.log(newArr, "newArr");
//=> [{"id":1,"name":["D","C","B","A"],"number":[5,4,3,1]},{"id":2,"name":"B","number":2}]


### 2、 两个数组的去重

let arr1 = [{id:1, name:'liming'},{id:2, name:'xiaohong'}]
let arr2 = [{id:3, name:'zhangfei'},{id:2, name:'xiaohong'}]
arr2.filter(el=> arr1.some(item=> item.id === el.id))
//=> [{id: 2, name: 'xiaohong'}]


## 四、总结

|
4月前
|

【面试题】数组去重的五种方法（必会）
【面试题】数组去重的五种方法（必会）
139 1
|
4月前
|

【面试题】数组去重的五种方法
【面试题】数组去重的五种方法
43 0
|
2月前
|
JavaScript 前端开发 C++
JavaScript 数组去重——普通数组去重 vs 对象数组去重
JavaScript 数组去重——普通数组去重 vs 对象数组去重
19 0
|
2月前
|
JavaScript
JS 数组去重（含简单数组去重【5种方法】、对象数组去重【2种方法】）
JS 数组去重（含简单数组去重【5种方法】、对象数组去重【2种方法】）
34 0
|
3月前
|
JavaScript 索引

18 0

43 0
|

82 0
|

94 0
|
4月前
|

javaScripe如何进行数组去重。
javaScripe如何进行数组去重。
22 0