alanhe421 / coding-note

note
3 stars 1 forks source link

数组去重 #57

Open alanhe421 opened 6 years ago

alanhe421 commented 6 years ago
    function deleteRepeat(arr) {
        var newArr = [];
        arr.forEach(it => {
            if (newArr.indexOf(it) < 0) {
                newArr.push(it);
            }
        });
        return newArr;
    }
    var kkk = [1, 2, 2, 1, 3, 5, 4, 5];
    kkk = deleteRepeat(kkk);

ECMA中所有函数的参数都是按值传递的。

alanhe421 commented 6 years ago

原型链

 var ttt = [1, 1, 1, 1, 14, 5];
    Array.prototype.distinct = function () {
        var newArr = [];
        this.forEach(it => {
            if (newArr.indexOf(it) < 0) {
                newArr.push(it);
            }
        });
        return newArr;
    };
    ttt = ttt.distinct();
    console.log(ttt);

Set

let aaa=[1,3,2,4,5,5,6,1];
const bbb=new Set(aaa);
aaa=Array.from(bbb);
console.log(aaa);

相关文档:Set object-MDN

alanhe421 commented 6 years ago

延伸

数组常用方法

Mutator-改值方法

Accessor-访问方法

迭代方法