shrink an array  [ 678 views ]

When you drop an element from an array by delete item[2];
The second item will be there with undefined value. Let’s delete more than one item from the array and there will be many holes in the line.
Here is a simple shrink function to solve this problem. This is a simple Array extension.

var shrink = function() {
  var ln = this.length;
  for(var i=0; i < ln; i++){
    if(this[i] != undefined){ this.push(this[i]); }
  }
  this.splice(0, ln);
  return this;
};
TypeExtend(Array, 'shrink', shrink);

I used a little bit strange but quick solution.
1. I need the original length of the Array
2. Push every defined (with value) item to the end of the Array
3. cut the original items from the start of the Array
4. the result is a clean Array

try this

var f = [1,2,3,4,5];
delete f[2];
delete f[4];
console.log(f);
// [1, 2, undefined × 1, 4, undefined × 1]
f.shrink();
console.log(f);
// [1, 2, 4] 

It's working nicely.

see also: extender – Array, javascript extender (TypeExtend)

#sidebar a { color:#fff; } #sidebar ul ul li { color: #DEF585; } #sidebar h2 { color: #fff; } #sidebar ul p, #sidebar ul select { color: #BEDDBE; } #backfly { background: url(images/golfBallWallPaper.jpg) left bottom fixed repeat-x #65a51d; }