单数组去重(日常记录)

206 阅读1分钟
    var arr = [1, 2, 3, 3, 4, 4, 5, 5, 6, 1, 9, 3, 25, 4];
    Array.prototype.unique = function () {
        var dict = Object.create(null);
        this.forEach(v => {
            if (!dict[v]) {
                dict[v] = true;
            }
        })
        return Object.keys(dict);
    }
    console.log(arr.unique())
    
    //实际开发Set
    

1.不需要使用插入排序思想splice,删元素了,开销大。
2.不需要创建另外一个数组双重循环比对新建数组中是否有相同值,indexOf等,加元素了,开销大。
3.从dict取key是字符串,需处理下,各有优缺吧。
4.掘金的排版是好看。