4

Consider the Code below:

function splicer()
{
 var arrayElements = ["elem1","elem2","elem3","elem4"];
 for(var index in arrayElements)
 {
  arrayElements.splice(index,1);
 }
 alert("Elements: "+arrayElements);
}

The above function is supposed to remove all the elements from the array "arrayElements". But it won't.

Javascript engine maintains the "index" as it is and doesn't mind the array being modified. People might expect something like "for each" loop that doesn't have this kind of issue

even the following code doesn't seem to work:

function splicer()
{
...
 for(var index in arrayElements)
 {
  arrayElements.splice(index--,1);
 }
...
}

even when changing the value of the variable "index" doesn't seem to work. the changed value is available inside the "for(...){...}" block but, as the loop reaches the next iteration, the value gets reset and continues from the next index as clockwork.

so it seems code like this might be the only solution:

function splicer()
{
 var arrayElements = ["elem1","elem2","elem3","elem4"];
 for(var index=0;index<arrayElements.length;index++)
 {
  arrayElements.splice(index--,1);
 }
 alert("Elements: "+arrayElements);
}

Tested in: Firefox 16 Beta.

But placing a unary Operator inside a "splice()" method seems to be misleading at first sight.

This might be worth considering to the "W3C" or whomever it may concern so that they come up with a nice solution.

Varun Sridharan
  • 1,983
  • 2
  • 20
  • 54
Nokia808Freak
  • 903
  • 9
  • 33

2 Answers2

2

You may want to refer to John Resig's array.remove() link.

// Array Remove - By John Resig (MIT Licensed)
Array.prototype.remove = function(from, to) {
  var rest = this.slice((to || from) + 1 || this.length);
  this.length = from < 0 ? this.length + from : from;
  return this.push.apply(this, rest);
};
Konstantin Dinev
  • 34,219
  • 14
  • 75
  • 100
  • D This piece of code Looks nice. But modifying the "out of the Box" Array Object doesn't sound like a good idea. and BTW the code snippet isn't user friendly at first sight. The guy who coded this might've carefully done this he must be really good in it... – Nokia808Freak Sep 28 '12 at 09:30
  • @CrystalPaladin the link provides you with an example where the prototype is not used. – Konstantin Dinev Sep 28 '12 at 11:01
0

Try this:

*Splice modifies the original array, hence tge loop skips the alternate values. *

var arrayElements = ["elem1","elem2","elem3","elem4"];
arrayElements.splice(0,arrayElements.length);
alert("Elements: "+arrayElements)
web-nomad
  • 6,003
  • 3
  • 34
  • 49
  • Not alternate values it just skips progressively. skips one for first iteration and two for second not including the first one it already skipped. and so on... – Nokia808Freak Sep 28 '12 at 09:14
  • I'm not emphasizing on emptying an array! I expected the viewers might think of a possible solution/explanation of the JavaScript behavior in such scenario – Nokia808Freak Sep 28 '12 at 11:17
  • If you see the splice docs - http://www.w3schools.com/jsref/jsref_splice.asp, it is clearly written that `This method changes the original array.` Let me create a fiddle for you which will hopefully make things clear. I'll post it in some time. – web-nomad Sep 28 '12 at 11:22