Monday, 25 January 2016

Remove an item from Array

To remove an item from Array, you need to call Array.splice().

I just feel that I can ease myself from memorizing this function name by adding "removeItem" function to the Array prototype like this:

if (!('removeItem' in Array.prototype)) {
    Array.prototype.removeItem = function (i) { this.splice(i, 1); };
}

So, next time when you want to remove an item from array, just call removeItem().

For example,

var a = [ "apple", "banana", "orange" ];
a.removeItem(1);

Then, "banana" will be removed from the array.

No comments:

Post a Comment

We are moving

We are moving this blog to our new blog site: https://ciysys.com/blog/nodejs.htm