3

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.

1

2 Answers 2

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);
};
Sign up to request clarification or add additional context in comments.

2 Comments

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...
@CrystalPaladin the link provides you with an example where the prototype is not used.
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)

3 Comments

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...
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
If you see the splice docs - 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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.