Jeff Jeff - 1 month ago 7
jQuery Question

Jquery - remove first item then select every second item in array with array.prototype

How would one remove 'RemoveThis' and then select every 0 (every 2nd item in array after removing the first item)?

var newItems = "RemoveThis,1,0,1,0,1,0,1,0,1,0,1,0",
everyThirdItem = newItems.split(",")
.filter(function(_, idx) { return ((idx + 1) % 2) === 0 });

console.log(everyThirdItem)

Answer

Use Array#slice to get remaining element except the first.

var newItems = "RemoveThis,1,0,1,0,1,0,1,0,1,0,1,0",
  everyThirdItem = newItems.split(",")
  .slice(1)
  .filter(function(_, idx) {
    return ((idx + 1) % 2) === 0
  });

console.log(everyThirdItem)


Or update the filter condition to avoid first element.

var newItems = "RemoveThis,1,0,1,0,1,0,1,0,1,0,1,0",
  everyThirdItem = newItems.split(",")
  .filter(function(_, idx) {
    return idx != 0 && idx % 2 === 0
  });

console.log(everyThirdItem)