javascript - How to remove all the odd indexes (eg: a[1],a[3]..) value from the array -


i have array var aa = ["a","b","c","d","e","f","g","h","i","j","k","l"]; wanted remove element place on index. ouput line aa = ["a","c","e","g","i","k"];

i tried in way

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

but not working.

you can remove alternate indexes doing this

var aa = ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l"];    (var = 0; < aa.length; i++) {    aa.splice(i + 1, 1);  }    console.log(aa);

or if want store in different array can this.

var aa = ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l"];    var x = [];  (var = 0; < aa.length; = + 2) {    x.push(aa[i]);  }    console.log(x);


Comments

Popular posts from this blog

Spring Boot + JPA + Hibernate: Unable to locate persister -

go - Golang: panic: runtime error: invalid memory address or nil pointer dereference using bufio.Scanner -

c - double free or corruption (fasttop) -