JavaScript数组去重的九种实现方案与原理详解
作者:闭包旅人时间:2026-10-30 23:35:42浏览:166
本文系统梳理了JavaScript中实现数组去重的九种主流方式,涵盖基础数据类型与对象数组场景。重点解析了利用Set、Map、indexOf、includes及各类循环结构的实现逻辑,对比了各方法在性能、代码简洁度及适用场景上的差异,为开发者提供全面的去重策略参考。
本文详细列举并解析了JavaScript中实现数组去重的九种不同方法,其中也包含了对对象数组的去重处理方案。
第一种方法:利用Array.from和Set进行去重
// 1.利用set去重
// Set是es6新增的数据结构,似于数组,但它的一大特性就是所有元素都是唯一的,没有重复的值,我们一般称为集合
// Array.from()就是将一个类数组对象或者可遍历对象转换成一个真正的数组,也是ES6的新增方法
let list = ['你是最棒的', 8, 8, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 7, 1, 2, 3, 4, 5, 6, 7, 8, '你是最棒的',]
let newList = Array.from(new Set(list))
console.log('newList', newList);运行效果如下所示:

第二种方法:利用includes进行去重
//2.利用Array.includes 去重
//includes() 方法用来判断一个数组是否包含一个指定的值,如果是返回 true,否则false。
let list = ['你是最棒的1', 8, 8, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 7, 1, 2, 3, 4, 5, 6, 7, 8, '你是最棒的1',]
let newList2 = []
list.forEach((item) => {
// 空数组newList2 不包含item为false ,取反为true 执行数组添加操作
// 如果数组包含了 item为true 取反为false 不执行数组添加操作
if (!newList2.includes(item)) {
newList2.push(item)
}
})
console.log('newList2', newList2);运行效果如下所示:

第三种方法:利用Map数据结构进行去重
//3.利用map去重
//map数据结构是es6中新出的语法,其本质也是键值对,只是其键不局限于普通对象的字符串
let list = ['你是最棒的2', 8, 8, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 7, 1, 2, 3, 4, 5, 6, 7, 8, '你是最棒的2',]
let newList3 = [];
let map = new Map()
list.forEach((item) => {
// 如果map.has指定的item不存在,那么就设置key和value 这个item就是当前map里面不存在的key,把这个item添加到新数组
// 如果下次出现重复的item,那么map.has(item等于ture 取反 !map.has(item) 不执行
if (!map.has(item)) {
map.set(item,ture)
newList3.push(item)
}
})
console.log('newList3', newList3);运行效果如下所示:

第四种方法:利用indexOf进行去重
//4.利用indexOf去重
//indexOf() 方法可返回某个指定的字符串值在字符串中首次出现的位置。如果没有找到匹配的字符串则返回 -1
let list = ['你是最棒的3', 8, 8, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 7, 1, 2, 3, 4, 5, 6, 7, 8, '你是最棒的3',]
let newList4 = [];
list.forEach((item) => {
// 空数组newList4第一次循环没有找到匹配的item 返回-1 执行数组添加操作
// 如果数组在第n次循环中找到了newList4数组中item 例如:item等于6 而在newList4数组中已经有9 所以indexOf就不等于-1 不执行数组添加操作
if (newList4.indexOf(item) === -1) {
newList4.push(item)
}
})
console.log('newList4', newList4);运行效果如下所示:

第五种方法:利用单层for循环进行去重
// 5.单层for循环去重
// Array.splice() 方法用于添加或删除数组中的元素。会改变原数组
let list = ['你是最棒的4', 8, 8, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 7, 1, 2, 3, 4, 5, 6, 7, 8, '你是最棒的4',]
let newlist5 = [];
for (let i = 0; i < list.sort().length; i++) {
if (list[i] == list[i + 1]) {
list.splice(i, 1)
i--
}
}
console.log('newlist5', list);运行效果如下所示:

第六种方法:利用双层for循环进行去重
// 6.双层for循环去重
// Array.splice() 方法用于添加或删除数组中的元素。会改变原数组
let list = ['你是最棒的5', 8, 8, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 7, 1, 2, 3, 4, 5, 6, 7, 8, '你是最棒的5',]
for (let i = 0; i < list.sort().length; i++) {
for (let j = i + 1; j < list.sort().length; j++) {
if (list[i] == list[j]) {
list.splice(i, 1)
j--
}
}
}
console.log('newlist6', list);运行效果如下所示:

第七种方法:利用递归逻辑进行去重
// 7.递归去重
let list = ['你是最棒的6', 8, 8, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 7, 1, 2, 3, 4, 5, 6, 7, 8, '你是最棒的6',]
function callBack(index) {
// 数组的长度不能等于0
if (index >= 1) {
// 第一次如果数组最后一个索引和最后第二个索引相等
if (list[index] === list[index - 1]) {
// 那么就删除这个索引的元素
list.splice(index, 1)
}
// 继续调用这个函数 第一次传入的参数是数组末尾第一个 第二次传入的参数是数组末尾第二个 层层递减
callBack(index - 1)
}
}
//传入排序好的数组的最大索引index
callBack(list.sort().length - 1)
console.log('newList7', list);运行效果如下所示:

第八种方法:利用Array.filter结合Map处理对象数组(性能较优)
//8.利用Array.filter和map去重
let map = new Map()
let list3 = [{
name: '好先森1',
id: 1
},
{
name: '好先森1',
id: 2
},
{
name: '好先森2',
id: 3
},
{
name: '好先森3',
id: 3
}
]
// 对象数组去重
function xxx(list3, key) {
return list3.filter((item) => !map.has(item[key].toString()) && map.set(item[key].toString()))
}
console.log('newList8', xxx(list3, 'id'));运行效果如下所示:

第九种方法:利用Array.filter结合Array.includes处理对象数组
// 9.利用Array.filter和Array.includes去重
let list4 = [{
name: '好先森1',
id: 1
},
{
name: '好先森1',
id: 2
},
{
name: '好先森2',
id: 3
},
{
name: '好先森3',
id: 3
}
]
function xxx(arr) {
let list = [];
return arr.filter((item) => !list.includes(item.name) && list.push(item.name))
}
console.log('newList9', xxx(list4));运行效果如下所示:

总结
以上就是关于JavaScript数组去重九种方式的详细介绍。如需了解更多相关技术内容,建议查阅本站过往文章或继续浏览后续系列教程,感谢大家的支持。
作者最新文章
什么时候该用JS构造器:与工厂模式、字面量的选择对比
2026-11-05 16:18:22
推荐几款轻量级JS照片墙开源库及对比
2026-11-05 15:56:19
JavaScript递归写法有哪些常见陷阱及避免方法
2026-11-05 14:53:38
JS数组浅拷贝与深拷贝怎么选?场景与方法总结
2026-11-05 14:41:02
p5.js 中 setup() 和 draw() 函数的执行机制详解
2026-11-05 12:32:38
