简体   繁体   English

我的删除功能有什么问题?

[英]What's wrong with my remove function?

Array.prototype.remove = function (obj) {
    for(var i = 0; i < this.length; i++) {
        if(this[i] === obj) {
            if (i == this.length) {
                this[i] = null;
            } else {
                for(var j = i; j < this.length-1; j++) {
                    this[j] = this[j+1];
                }
                delete this[j]; // updated from this[j] = null; still not working.
            }
        }
    }
    return this;
};

calling it with: 用它来调用:

write("ARRAY TEST = " + [22, 33, 44].remove(33).remove(22));

..it prints: ..打印:

44,,

Why this 2 commas and how to fix my remove function to remove the commas as well? 为什么这两个逗号以及如何修复我的删除功能以删除逗号呢?

delete on an Array will not remove the element, it will set it to undefined . delete Array不会删除元素,它会将其设置为undefined And since undefined when printed results in an empty string, that explains the results of write() . 并且由于打印时undefined导致空字符串,这解释了write()的结果。

You need to use splice() to remove the element. 您需要使用splice()来删除元素。 If you combine it with indexOf (you may need to define it for older browser) you get a pretty short function: 如果将它与indexOf结合使用(您可能需要为旧浏览器定义它),您将得到一个非常短的函数:

Array.prototype.remove = function (obj) {
    this.splice(this.indexOf(obj), 1);
    return this;
}

PS: I'm not an advocate of expanding native prototypes... PS:我不是扩大原生原型的倡导者......

将项设置为null会在数组中留下一个项(但它是一个空项),这就是为什么你仍然看到逗号的原因。

As previously mentioned, deleting or setting the item to null still leaves the item in the array. 如前所述,删除或将项设置为null仍会将项留在数组中。 What you want to use is Array.splice 你想要使用的是Array.splice

Here's an implementation that should work: 这是一个应该工作的实现:

Array.prototype.remove = function (obj) {
    for(var i = 0; i < this.length; i++) {
       if(this[i] === obj)
       {
           this.splice(i,1);
           break;
       }       
    }
    return this;
};

You don't remove the elements from the array you just set them to null. 您不会从刚刚设置为null的数组中删除元素。 If you need inspiration look at this remove method. 如果您需要灵感,请查看此删除方法。 It's by index and not by element. 它是索引而不是元素。

http://ejohn.org/blog/javascript-array-remove/ http://ejohn.org/blog/javascript-array-remove/

try: 尝试:

Array.prototype.remove = function (obj) {
    for(var i = 0; i < this.length; i++) {
        if(this[i] === obj) {
            if (i == this.length) {
                this.splice(i,1);
            } else {
                for(var j = i; j < this.length-1; j++) {
                    this[j] = this[j+1];
                }
                this.splice(j,1);
            }
        }
    }
    return this;
};
Array.prototype.remove = function (obj) {
    for(var i = 0; i < this.length; i++) {
        if(this[i] === obj) {
            if (i == this.length) {
                #this[i] = null;
                delete this[i];
            } else {
                for(var j = i; j < this.length-1; j++) {
                    this[j] = this[j+1];
                }
                #this[j] = null;
                delete this[i];
            }
        }
    }
    return this;
};

Pretty sure that is what you want 很确定这就是你想要的

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM