I am looking to remove every second and third element of an array in Javascript.
The array I am working with is as follows:
var fruits = ["Banana", "yellow", "23", "Orange", "orange", "12", "Apple", "green", "10"];
My goal is to delete every second and third element to achieve the following result:
["Banana", "Orange", "Apple"]
I attempted using a for-loop along with splice like this:
for (var i = 0; fruits.length; i = i+3) {
fruits.splice(i+1,0);
fruits.splice(i+2,0);
};
However, this approach gives me an empty array because the elements are being removed while the loop is still running.
Can anyone guide me on how to correctly accomplish this task?