every other item in array javascript code example
Example 1: javascript every other element in array
let arr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
let x = arr.filter((element, index) => {
return index % 2 === 0;
})
console.log(x)
Example 2: get every other item in an array
var myArray = ["First", "Second", "Third", "Fourth", "Fifth"]
function every_other(array){
var temporaryArray = []
for (var i = 0; i < array.length; i += 2){
temporaryArray.push(array[i])
}
return temporaryArray.join(", ")
}
console.log(every_other(myArray))