1
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?

The Student
  • 27,520
  • 68
  • 161
  • 264

5 Answers5

3

delete on an Array will not remove the element, it will set it to undefined. And since undefined when printed results in an empty string, that explains the results of write().

You need to use splice() to remove the element. If you combine it with indexOf (you may need to define it for older browser) you get a pretty short function:

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

PS: I'm not an advocate of expanding native prototypes...

Ivo Wetzel
  • 46,459
  • 16
  • 98
  • 112
  • It works, thaks! I just thought it strange that [this doc](http://www.w3schools.com/jsref/jsref_slice_array.asp) says that slice second arg is "end", not "length" as it actually is. – The Student Feb 02 '11 at 19:50
  • @Tom For future reference stick to MDN, w3schools is outdated and has a lot of misinformation. – Ivo Wetzel Feb 02 '11 at 19:51
1

Setting the item to null leaves an item in the array (but it is a null item), which is why you see the commas still.

Mark Avenius
  • 13,679
  • 6
  • 42
  • 50
1

As previously mentioned, deleting or setting the item to null still leaves the item in the array. What you want to use is 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;
};
Damp
  • 3,328
  • 20
  • 19
0

You don't remove the elements from the array you just set them to 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/

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;
};
alexl
  • 6,841
  • 3
  • 24
  • 29
-1
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

JohnO
  • 1,889
  • 1
  • 12
  • 15
  • 1
    I think you mean `delete this[j]`, but it don't work, the commas are still there.. – The Student Feb 02 '11 at 19:31
  • -1 delete does not remove the element from the array (http://stackoverflow.com/questions/206988/how-do-i-unset-an-element-in-an-array-in-javascript) – Damp Feb 02 '11 at 19:38