Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

javascript every other element in array

let arr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];

// filter out all elements that are located at an even index in the array.

let x = arr.filter((element, index) => {
  return index % 2 === 0;
})

console.log(x) 
// [1, 3, 5, 7, 9]
Comment

get every other item in an array

// 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
Comment

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
Comment

PREVIOUS NEXT
Code Example
Javascript :: angular generate module with rooting 
Javascript :: how to delete a variable in js 
Javascript :: react-bootstrap navbar nav link refreshes page 
Javascript :: xhr post send 
Javascript :: mongoose update createdAt 
Javascript :: node eventemitter emit error 
Javascript :: javascript string unique characters 
Javascript :: connecting react to socket.io 
Javascript :: check if localstorage key exists js 
Javascript :: string replace in javascript 
Javascript :: alert.alert react native style 
Javascript :: code Execution time in nodejs 
Javascript :: react native disable the text input 
Javascript :: how to check if browser tab is active javascript 
Javascript :: vscode regex replace only group 
Javascript :: how to divide array in two parts in js 
Javascript :: get id of clicked element javascript 
Javascript :: content editable vuejs 
Javascript :: push-method-in-react-hooks-usestate 
Javascript :: react native position text in center of view 
Javascript :: safeareaview react native 
Javascript :: boucle for javascript 
Javascript :: postman test check response status 
Javascript :: smooth scroll to target with offset 
Javascript :: find duplicates in array javascript 
Javascript :: how to check if function is running js 
Javascript :: object exists in array javascript 
Javascript :: putting a loop into an array javascript 
Javascript :: angular elementref 
Javascript :: vue dynamic component props 
ADD CONTENT
Topic
Content
Source link
Name
8+1 =