Recently I've found myself writing a ton of client code in a new pure JavaScript/WCF project I am working on. Since most of the programming I'm doing for the UI is in JavaScript files, I've found myself having to work with the Array object for many different things. The one thing I've noticed however, is that you cannot remove an item from a JavaScript array, say, in the middle of the array and have the array size shrink without losing data at either end, however having to check for array_name[i] != null every single time I iterate the array was getting annoying, to say the least.
I decided to flex the muscle of the prototype, which in JavaScript allows you to extend a types method and properties. This new method I created, called shrink(), aptly removes all of the nulls from my array and shrinks the total length without losing any data. I'm not sure how efficient it is, but it works and I no longer have to check for null where at least it doesn't make sense to anyway.
Array.prototype.shrink = function() {
var sync = new Array();
while (this.length > 0) {
var obj = this.shift();
if (obj !== null) sync.push(obj);
}
while (sync.length > 0) {
var obj = sync.shift();
if (obj !== null) this.push(obj);
}
}