I want to remove every second and third element from an array in Javascript.
My array looks like this:
var fruits = ["Banana", "yellow", "23", "Orange", "orange", "12", "Apple", "green", "10"];
Now I want to remove every second and third element. The result will look like this:
["Banana", "Orange", "Apple"]
I tried using a for loop and splice:
for (var i = 0; fruits.length; i = i+3) { fruits.splice(i+1,0); fruits.splice(i+2,0); };
Of course, this returns an empty array, because the elements are deleted while the loop is still running.
How can I do it right?
javascript arrays splice
Bob
source share