Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

javascript how to remove every 3rd element from an array

how do i remove every 3rd element from an array?

var TheArray = ['h', 'e', 'z', 'l', 'l', 'l', 'o']

How do I make this say "hello" without creating a new array?

like image 242
Curiously Avatar asked Mar 20 '13 20:03

Curiously


1 Answers

Try this:

for (var i = 2; i <= TheArray.length; i += 2)
    TheArray.splice(i, 1);

If you want a string in the end, just use TheArray.join("").

like image 116
MaxArt Avatar answered Sep 24 '22 20:09

MaxArt