Howto delete every second and third element from an array?

You could approach this from a different angle and push() the value you don't want deleted into another Array:

var firstFruits = []

for (var i = 0; i < fruits.length; i = i+3) {
    firstFruits.push(fruits[i]);
};

This approach may not be as terse as using splice(), but I think you see gain in terms of readability.


This works for me.

var fruits = ["Banana", "yellow", "23", "Orange", "orange", "12", "Apple", "green", "10","Pear","something","else"];

for(var i = 0; i < fruits.length; i++) {
    fruits.splice(i+1,2);
}

//fruits = Banana,Orange,Apple,Pear

Here's a demo that illustrates it a little better: http://jsfiddle.net/RaRR7/


Try looping through the array in reverse order


You could use filter:

var filtered = [
   "Banana", 
   "yellow", 
   "23", 
   "Orange", 
   "orange", 
   "12", 
   "Apple", 
   "green", 
   "10"
].filter(function(_, i) {
    return i % 3 === 0;
})

Returns:

["Banana", "Orange", "Apple"]