Javascript RemovedUpplicates无法按预期工作

Javascript RemovedUpplicates无法按预期工作,javascript,arrays,Javascript,Arrays,我试图通过以下功能扩展阵列原型: Array.prototype.uniqueItems=function(){ var ar=this,unique=[],max=ar.length; for(var i = 0 ; i < max;i++) if(unique.indexOf(ar[i] )==-1) unique.push(ar[i]); return unique; }; Array.prototype.remov

我试图通过以下功能扩展阵列原型:

Array.prototype.uniqueItems=function(){
    var ar=this,unique=[],max=ar.length;
    for(var i = 0 ; i < max;i++)
        if(unique.indexOf(ar[i] )==-1)
            unique.push(ar[i]);
    return unique;  
};
Array.prototype.removeDuplicates=function(){
    var self = this;
    self = this.uniqueItems();
    return self;
};
输出为: [ 1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 4, 4 ] 所以复制品仍然保留。有什么想法吗?提前谢谢!!!!还请注意,我不能使用
x=x.uniqueItems()
,因为它只对x有效。我希望它对任何给定数组有效。

重新分配给x

var x=[1,1,1,1,2,2,2,2,3,3,3,3,4,4];
x = x.removeDuplicates();
console.log(x);
重新分配给x

var x=[1,1,1,1,2,2,2,2,3,3,3,3,4,4];
x = x.removeDuplicates();
console.log(x);
当你说

self = this.uniqueItems();
self
现在指向一个新数组,而不是
this

你要么做,要么做

x = x.removeDuplicates();
或者在
removeDuplicates
中再次迭代
,以删除其他项

比如说

Array.prototype.removeDuplicates=function(){
    var self = this;
    var that = this;
    self = this.uniqueItems();
    //remove all items
    for(var counter = that.length ; counter >= 0; counter -- )
    {
      that.splice(counter,1);
    }
    //copy the individual items to this
    Object.keys(self).forEach(function(index){
      that[index] = self[index]
    });
    return self;
};
当你说

self = this.uniqueItems();
self
现在指向一个新数组,而不是
this

你要么做,要么做

x = x.removeDuplicates();
或者在
removeDuplicates
中再次迭代
,以删除其他项

比如说

Array.prototype.removeDuplicates=function(){
    var self = this;
    var that = this;
    self = this.uniqueItems();
    //remove all items
    for(var counter = that.length ; counter >= 0; counter -- )
    {
      that.splice(counter,1);
    }
    //copy the individual items to this
    Object.keys(self).forEach(function(index){
      that[index] = self[index]
    });
    return self;
};

嘿,谢谢大家的回答。我通过以下操作找到了解决问题的简单方法:

Array.prototype.removeDuplicates=function(){
    //First create a copy of the current array without duplicates
    var clear_array=this.uniqueItems();
    //Delete every item in the current array
    this.length=0;
    //Fill the current array with the elements of clear_array
    for(var i = 0;i<clear_array.length;i++)
        this[i] = clear_array[i];
    return this;
};
Array.prototype.removeDuplicates=function(){
//首先创建当前阵列的副本,不包含重复项
var clear_array=this.uniqueItems();
//删除当前数组中的所有项
这个。长度=0;
//用clear_数组的元素填充当前数组

对于(var i=0;i嘿,谢谢大家的回答。我通过以下操作找到了解决问题的简单方法:

Array.prototype.removeDuplicates=function(){
    //First create a copy of the current array without duplicates
    var clear_array=this.uniqueItems();
    //Delete every item in the current array
    this.length=0;
    //Fill the current array with the elements of clear_array
    for(var i = 0;i<clear_array.length;i++)
        this[i] = clear_array[i];
    return this;
};
Array.prototype.removeDuplicates=function(){
//首先创建当前阵列的副本,不包含重复项
var clear_array=this.uniqueItems();
//删除当前数组中的所有项
这个。长度=0;
//用clear_数组的元素填充当前数组

对于(var i=0;i您不妨在ES6中这样做

var x=[1,1,1,2,2,2,3,3,3,4,4],
u=a=>Array.from(新集合(a)),
y=u(x);

console.log(y);
您不妨在ES6中这样做

var x=[1,1,1,2,2,2,3,3,3,4,4],
u=a=>Array.from(新集合(a)),
y=u(x);

console.log(y);
数组
转换为a并返回,可以实现更简洁的实现:


数组
转换为a和B可以实现更简洁的实现:


x=x.removeDuplicates()
。该方法返回一个新数组,而不是修改原始数组。您必须将其存储在某个位置。
x=x.removeDuplicates()
。该方法返回一个新数组,而不是修改原始数组。您必须将其存储在某个位置。您能解释一下您的意思吗“在RemovedUpplicates中再次迭代此项以删除其他项”?您能解释一下“在RemovedUpplicates中再次迭代此项以删除其他项”的意思吗?这是最简单的方法,但不是;)。检查更新的问题这是最简单的方法,但不是;)。查看更新的问题。您是否介意解释一下?这将有助于未来的访问者了解此代码does@cssGEEK是的,你是对的……我刚刚对我的答案添加了一个解释。还请注意,我不希望这种情况只发生在x上。如果我有多个数组,我希望在调用removeDuplicates()时使用它们如果你想检查我自己的答案,@cssGEEK你可以在
u
函数中加入任何数组,只要它的项是像整数或字符串这样的原始类型,你就会得到返回并用统一的项进行数组。你介意解释一下吗?这对未来的访问者来说是一个好消息,让他们知道这个c是什么颂歌does@cssGEEK是的,你是对的……我刚刚对我的答案添加了一个解释。还请注意,我不希望这种情况只发生在x上。如果我有多个数组,我希望在调用removeDuplicates()时使用它们如果您想检查我自己的答案。@cssGEEK您可以将任何数组放入
u
函数中,如果它的项是原始类型(如整数或字符串),则返回并使用统一项进行数组。