Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR JAVASCRIPT

get every other item in an array

// If you want every odd index value:
var myArray = ["First", "Second", "Third", "Fourth", "Fifth"]
function every_other(array){
    var temporaryArray = []
    for (var i = 0; i < array.length; i += 2){ //Add two to i every iteration
        temporaryArray.push(array[i]) //Add the element at index i to a temporary array
    }
    return temporaryArray.join(", ")
}
console.log(every_other(myArray)) //Expected output: First, Third, Fifth
Source by stackoverflow.com #
 
PREVIOUS NEXT
Tagged: #item #array
ADD COMMENT
Topic
Name
9+9 =