我有一个包含重复值的数组
let ary = [5, 1, 3, 5, 7, 8, 9, 9, 2, 1, 6, 4, 3];
我想将重复值设置为 0:
[0, 0, 0, 0, 7, 8, 0, 0, 2, 0, 6, 4, 0]
可以找出重复值,但我想将重复值改为0,有什么更好的方法吗?
let ary = [5, 1, 3, 5, 7, 8, 9, 9, 2, 1, 6, 4, 3];
Array.prototype.duplicate = function () {
let tmp = [];
this.concat().sort().sort(function (a, b) {
if (a == b && tmp.indexOf(a) === -1) tmp.push(a);
});
return tmp;
}
console.log(ary.duplicate()); // [ 1, 3, …Run Code Online (Sandbox Code Playgroud)