Sunday, May 19, 2024
 Popular · Latest · Hot · Upcoming
107
rated 0 times [  114] [ 7]  / answers: 1 / hits: 15606  / 8 Years ago, mon, august 8, 2016, 12:00:00

I have an array like var aa = [a,b,c,d,e,f,g,h,i,j,k,l]; I wanted to remove element which is place on even index. so ouput will be line aa = [a,c,e,g,i,k];



I tried in this way



for (var i = 0; aa.length; i = i++) {
if(i%2 == 0){
aa.splice(i,0);
}
};


But it is not working.


More From » arrays

 Answers
215

you can remove all the alternate indexes by doing this





var aa = [a, b, c, d, e, f, g, h, i, j, k, l];

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

console.log(aa);






or if you want to store in a different array you can do like this.





var aa = [a, b, c, d, e, f, g, h, i, j, k, l];

var x = [];
for (var i = 0; i < aa.length; i = i + 2) {
x.push(aa[i]);
}

console.log(x);




[#61103] Friday, August 5, 2016, 8 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
jakobarmandr

Total Points: 363
Total Questions: 103
Total Answers: 87

Location: Romania
Member since Mon, Jun 6, 2022
2 Years ago
;