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.

3
  • 6
    aa = aa.filter(function (v,i) { return !(i%2); }); Aug 8, 2016 at 11:24
  • @Rajesh, no it will not..
    – Aleksey L.
    Aug 8, 2016 at 11:30
  • no matter what you do, unless you shrink the array to zero length, the for loop will run forever or until there's an exception ... aa.length will always be "truthy" Aug 8, 2016 at 11:32

7 Answers 7

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);

2
  • 1
    to update existing array, just use aa = aa.filter instead of var res = aa.filter Aug 8, 2016 at 11:31
  • look at the question - no reference required Aug 8, 2016 at 11:36
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);

1
  • ya, Is it going to infinite loop for you ?
    – UDID
    Aug 8, 2016 at 11:43
4

You can use .filter()

 aa =  aa.filter((value, index) => !(index%2));
1
  • 2
    aa = aa. ... as filter returns a new array, does not change the array itself Aug 8, 2016 at 11:27
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;
0
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)
1
  • 2
    can you elaborate on your answer? it seems incomplete.
    – cloned
    Sep 12, 2019 at 11:04
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).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.