前端日常开发常用功能系列之数组去重

这是前端日常开发常用功能这个系列文章的第一篇,该系列是日常开发中一些常用的功能的再总结、再提炼,以防止遗忘,便于日后复习。该系列预计包含以下内容: 防抖、节流、去重、拷贝、最值、扁平、偏函数、柯里、惰性函数、递归、乱序、排序、注入、上传、下载、截图。。。

本文所记录的数组去重方法只针对一维数组,且数组项都是基本数据类型值

方法一: 二重循环比较去重(因为使用的是‘===’比较,适合数组项为数值、字符串的数组)

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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,6,7,4,1,1,2,3,5,3,1,5,6,7,4,1,1,2,3,5,3,1,5,6,7,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];
    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

前端日常开发常用功能系列之数组去重的相关文章

前端日常开发常用功能系列之节流

这是前端日常开发常用功能这个系列文章的第一篇,该系列是日常开发中一些常用的功能的再总结.再提炼,以防止遗忘,便于日后复习.该系列预计包含以下内容: 防抖.节流.去重.拷贝.最值.扁平.偏函数.柯里.惰性函数.递归.乱序.排序.注入.上传.下载.截图...  什么是节流? 节流就是如果你持续触发事件,每隔一段时间,只执行一次事件. 为什么要进行节流? 在频繁触发事件的场景,有些情况可能执行的逻辑比较复杂或者耗时,此时浏览器的处理跟不上触发,就会发生卡顿.假死或者事件堆积,为了解决这个故障,节流是其

PHP微信公众号开发常用功能

最近学习了关于微信公众号开发的相关知识,为了帮助自己更好的理解,在此重新再梳理一遍 更多关于微信公众号开发的功能可以参考微信公众平台的开发技术文档 完成开发者配置 第一步,需要在微信公众平台配置我们的服务器  在接口的文件需要写入以下代码以完成验证: class Wxapi { public function __construct() { $this->index(); } public function index() { $echostr = isset($_GET['echostr'])

web前端面试系列 - 算法( 数组去重 )

1. 思路:设置一个临时数组temp,然后遍历要去重的数组arr,如果arr中的元素能够在temp中找到,则跳过此元素,否则将此元素存入temp,最后返回temp. 实现一 1 function unique(arr){ 2 var temp = []; 3 var len = arr.length; 4 for(var i = 0; i < len; i++){ 5 if(temp.indexOf(arr[i]) === -1) temp.push(arr[i]); 6 } 7 return

ios 日常开发常用宏定义

  #pragma mark - 字体.颜色相关 #define kFONT_SIZE(f) [UIFont systemFontOfSize:(f)] #define kFONT_BOLD_SIZE(f) [UIFont boldSystemFontOfSize:(f)] #define kFONT_ITALIC_SIZE(f) [UIFont italicSystemFontOfSize:(f)] #define kRGBCOLOR(r,g,b) [UIColor colorWithRed:

【原创】日常开发常用操作(不定时更新)

1.服务端抓包 tcpdump tcp port 18888 -vv -x -i eth0 root执行,抓包后分析时注意每个包前20字节是ip首部,其中1-2字节是版本号首部长度服务号等一般是4500,其中第3-4字节是整个包大小(=40+包体数据),剩下由源目标地址,接着20字节是tcp首部,有端口等信息. 所以我们的真正数据时从第三行第五段开始的. 另外int的话 xxyy zzqq  显示的话实际是 zzqq xxyy 另外会有粘包 或者 半包的情况,所以要留意长度等 2.进制转换 ec

javascript实现数组去重

数组去重即去掉数组中重复的元素,是web前端工作者在面试过程中和工作中经常会遇到的一个问题,相信一般的方法大家都会,只不过会存在效率差异问题,下面我就来说说我们常用的几种数组去重的方法! 方法1,思路:通过双重循环,每次检测新数组里面是否有该元素,没有就加进去,改方法思路简单,但当数组长度较长时,会非常影响性能!(不推荐使用) function deleteRepeat1(arr) { var newArr = []; for (var i = 0; i < arr.length; i++) {

JavaScript数组去重方法及测试结果

最近看到一些人的去面试web前端,都说碰到过问JavaScript数组去重的问题,我也学习了一下做下总结. 实际上最有代表性也就三种方法:数组双重循环,对象哈希,排序后去重. 这三种方法我都做了性能测试,有兴趣的朋友可以去试试,这是测试地址 双重循环最慢(不是一般的慢),对象哈希是最快的,排序后去重根据浏览器不同比对象哈希慢2-3倍

使用reduce的方法实现对象数组去重

在开发中和面试当中,数组去重问题往往是受宠儿,那用最短的代码解决这个问题会使效率得到更大的提升.普通的数组,我们可以通过filter过滤方法进行去重,详情见本人博客:http://www.cnblogs.com/mmykdbc/p/8422419.html 那当是一个对象数组的时候呢,filter就显得相形见绌了,再用那种笨的一个一个遍历的方法会使得工作效率大打折扣,这个时候,需要用到reduce叠加方法,上代码: let arr = [{"all": 1}, {"all&q

前端开发掌握nginx常用功能之rewrite

上一篇博文对nginx最常用功能的server及location的匹配规则进行了讲解,这也是nginx实现控制访问和反向代理的基础.掌握请求的匹配规则算是对nginx有了入门,但是这些往往还是不能满足实际的需求场景,例如请求url重写.重定向等等,这都需要对请求的path进行修改操作的,匹配规则是不能独自完成实际需求的,这就需要掌握nginx的另一个常用功能rewrite,下面就来说说这个常用功能. Rewrite规则 rewrite功能就是,使用nginx提供的全局变量或自己设置的变量,结合正