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// If you want every even index value:
var myArray = ["First", "Second", "Third", "Fourth", "Fifth"]
function every_other(array){
var temporaryArray = []
for (var i = 1; 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: Second, Fourth
Also in JavaScript:
- mongoose sparse index
- javascript splice without changing array
- remove extra space in string javascript
- input output ionic
- how to send message to specific client using ws node js
- javascript enumerate with index
- chartjs how to disable animation
- loop though json object in javascript
- string concat javascript
- javascript ajax post send an object
- afficher une variable dans la console javascript
- react native cover image in parent view
- jquery get textarea value
- javascript submit form when enter key pressed
- firebase cloud functions send email
- local storage javascript array
- js onclick open the phone application
- output event angular
- railsparse json to object
- how to check chrome version in js
- How to use hash in javascript
- detect if two line segments intersect each other javascript
- Javascript get caller function
- js use await in synchronous method