关于前端:编程篇004请给-Array-本地对象增加一个原型方法用于删除数组中重复的条目并按升序排序返回值是被删除条目的新数组

9次阅读

共计 303 个字符,预计需要花费 1 分钟才能阅读完成。

参考答案:

Array.prototype.distinct = function() {var ret = [];
    for (var i = 0; i < this.length; i++) {for (var j = i + 1; j < this.length;) {if (this[i] === this[j]) {ret.push(this.splice(j, 1)[0]);
            } else {j++;}
        }
    }
    return ret;
};
let arr = ["a", "b", "c", "d", "b", "a", "e"];
console.log(arr.distinct()); // ['a', 'b']
console.log(arr); // ['a', 'b', 'c', 'd', 'e']
正文完
 0