Jeff Jeff - 25 days ago 8
jQuery Question

Jquery .split(",") then show every :nth-child(2)

How could one find every 3rd item in an array after .split()?

newItems = "1,1,0,1,1,0,1,1,0,1,1,0"

shows just the first 0:

var test = ("" + newItems).split(",")[2];


I am trying to do something like this and show 0,0,0,0:

var test = ("" + newItems).split(",")[:nth-child(2)];

Answer

Use Array.prototype.filter()

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

console.log(everyThirdItem)

Comments