13

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.

shanky singh
  • 1,121
  • 2
  • 12
  • 24

7 Answers7

18

Use Array#filter method

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

var res = aa.filter(function(v, i) {
  // check the index is odd
  return i % 2 == 0;
});

console.log(res);

If you want to update existing array then do it like.

var aa = ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l"],
    // variable for storing delete count
  dCount = 0,
    // store array length
  len = aa.length;

for (var i = 0; i < len; i++) {
  // check index is odd
  if (i % 2 == 1) {
    // remove element based on actual array position 
    // with use of delete count
    aa.splice(i - dCount, 1);
    // increment delete count
    // you combine the 2 lines as `aa.splice(i - dCount++, 1);`
    dCount++;
  }
}


console.log(aa);

Another way to iterate for loop in reverse order( from last element to first ).

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

// iterate from last element to first
for (var i = aa.length - 1; i >= 0; i--) {
  // remove element if index is odd
  if (i % 2 == 1)
    aa.splice(i, 1);
}


console.log(aa);
Pranav C Balan
  • 113,687
  • 23
  • 165
  • 188
13

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);
Pranav C Balan
  • 113,687
  • 23
  • 165
  • 188
UDID
  • 2,350
  • 3
  • 16
  • 31
4

You can use .filter()

 aa =  aa.filter((value, index) => !(index%2));
James
  • 1,436
  • 1
  • 13
  • 25
2

You can use temporary variable like below.

var a = [1,2,3,4,5,6,7,8,9,334,234,234,234,6545,7,567,8]

var temp = [];
for(var i = 0; i<a.length; i++)
   if(i % 2 == 1)
      temp.push(a[i]);

a = temp;
Fatih TAN
  • 778
  • 1
  • 5
  • 23
1

in Ecmascript 6,

var aa = ["a","b","c","d","e","f","g","h","i","j","k","l"];
var bb = aa.filter((item,index,arr)=>(arr.splice(index,1)));
console.log(bb);
0
const aa = ["a","b","c","d","e","f","g","h","i","j","k","l"];
let bb = aa.filter((items, idx) => idx % 2 !== 0)
Abbos
  • 1
  • 1
0

I read here that splice has O(N) time complexity. Don't use it in a loop!

A simple alternative for removing odd indexes in place:

for (let idx = 0; idx < aa.length; idx += 2)
    aa[idx >> 1] = aa[idx];
aa.length = (aa.length + 1) >> 1;

I use x >> 1 as a shortcut to Math.floor(x/2).

RaphaMex
  • 2,781
  • 1
  • 14
  • 30