这是前端日常开发常用功能这个系列文章的第一篇,该系列是日常开发中一些常用的功能的再总结、再提炼,以防止遗忘,便于日后复习。该系列预计包含以下内容: 防抖、节流、去重、拷贝、最值、扁平、偏函数、柯里、惰性函数、递归、乱序、排序、注入、上传、下载、截图。。。
本文所记录的数组去重方法只针对一维数组,且数组项都是基本数据类型值
方法一: 二重循环比较去重(因为使用的是‘===’比较,适合数组项为数值、字符串的数组)
const unique1 = arr => { const tempArr = arr.concat(); for(let i = 0; i < tempArr.length; i++) { for(let j = i + 1; j < tempArr.length; j++) { if(tempArr[i] === tempArr[j]){ tempArr.splice(j, 1); j--; } } } return tempArr; };
方法二: 使用indexOf返回查找元素首次出现位置的特性
const unique2 = arr => { const tempArr = arr.concat(); for(let i = 0; i < tempArr.length; i++) { if(tempArr.indexOf(tempArr[i]) !== i) { tempArr.splice(i, 1); i--; } } return tempArr; };
方法三: 使用数组的filter方法
const unique3 = arr => { const tempArr = arr.concat(); return tempArr.filter((item, index, self) => { return self.indexOf(item) === index; }); };
方法四:利用对象键特性(该方法不能区分类似这样的数组项 1 和 ‘1’)
const unique4 = arr => { const tempArr = arr.concat(); const tempObj = {}; for(let i = 0; i< tempArr.length; i++) { if(!tempObj[tempArr[i]]) { tempObj[tempArr[i]] = true; } else { tempArr.splice(i, 1); i--; } } return tempArr; };
方法五:利用es6 set数据结构的特性
const unique5 = arr => { return Array.from(new Set(arr)); };
方法六: 利用数组原型的includes方法
const unique6 = arr => { const tempArr = []; for(let i = 0; i < arr.length; i++) { if(!tempArr.includes(arr[i])) { tempArr.push(arr[i]); } } return tempArr; };
不妨简单测试一下这几种方法的性能,使用一个较大的数组
const ar = [1,1,2,3,5,3,1,5,6,7,4,1,1,2,3,5,3,1,5,6,7,4,1,1,2,3,5,3,1,5,6,7,4,1,1,2,3,5,3,1,5,6,7,4,1,1,2,3,5,3,1,5,6,7,4,1,1,2,3,5,3,1,5,6,7,4,1,1,2,3,5,3,1,5,6,7,4,1,1,2,3,5,3,1,5,6,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5,6,7,4,1,1,2,3,5,3,1,5,6,7,4,1,1,2,3,5,3,1,5,6,7,4,1,1,2,3,5,3,1,5,6]; console.time(‘test1‘); let temp1 = unique1(ar); console.timeEnd(‘test1‘); console.time(‘test2‘); let temp2 = unique1(ar); console.timeEnd(‘test2‘); console.time(‘test3‘); let temp3 = unique1(ar); console.timeEnd(‘test3‘); console.time(‘test4‘); let temp4 = unique1(ar); console.timeEnd(‘test4‘); console.time(‘test5‘); let temp5 = unique1(ar); console.timeEnd(‘test5‘); console.time(‘test6‘); let temp6 = unique1(ar); console.timeEnd(‘test6‘); console.log(temp1, temp2, temp3, temp4, temp5, temp6);
测试结果大概如下
可以看到这6种方法都得到了正确的结果
方法1使用了双重循环,不出意料的相对较慢
但同时注意到splice的性能似乎并不好。。。
原文地址:https://www.cnblogs.com/innooo/p/10432556.html
时间: 2024-10-11 02:10:50