get every other item in an array

Solutions on MaxInterview for get every other item in an array by the best coders in the world

showing results for - "get every other item in an array"
Said
26 Jul 2019
1let arr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
2
3// filter out all elements that are located at an even index in the array.
4
5let x = arr.filter((element, index) => {
6  return index % 2 === 0;
7})
8
9console.log(x) 
10// [1, 3, 5, 7, 9]
Frida
26 Mar 2016
1// If you want every odd index value:
2var myArray = ["First", "Second", "Third", "Fourth", "Fifth"]
3function every_other(array){
4    var temporaryArray = []
5    for (var i = 0; i < array.length; i += 2){ //Add two to i every iteration
6        temporaryArray.push(array[i]) //Add the element at index i to a temporary array
7    }
8    return temporaryArray.join(", ")
9}
10console.log(every_other(myArray)) //Expected output: First, Third, Fifth
Manuel
17 Jan 2021
1// If you want every even index value:
2var myArray = ["First", "Second", "Third", "Fourth", "Fifth"]
3function every_other(array){
4    var temporaryArray = []
5    for (var i = 1; i < array.length; i += 2){ //Add two to i every iteration
6        temporaryArray.push(array[i]) //Add the element at index i to a temporary array
7    }
8    return temporaryArray.join(", ")
9}
10console.log(every_other(myArray)) //Expected output: Second, Fourth
queries leading to this page
how to add an index to every other index in the array in javascriptjavascript include every second element in arrayhow to access every other index in an arrayjs add every other number in arrayhow to find every other el in the array jsget every other item from arrayloop every other number in array javascriptfor loop every other jsjavascript every other element in arrayhow to filter every other element in an array with javascriptget every other item in an arrayslice every other element jsevery item of one array should match another array javascripthow to perform operation on every other value in array jsget every other item from list into new array javascripthow to look at every other item in an array for loophow to look at every other item in an arrayslice out every 2nd item in the array jsjavascript slice every otherevery other item in array javascriptjavsctip insert object every other 3rdselect every other wtring in jsjavascript join array every other elementjavsctip insert object every otherget every other element in list javascriptjavascript every other elementevery other umber in a js arrayjavascript insert object in arrayevery other 3rdjavascript array get every other elementhow to map through every other object in an array javascriptaccessing every other element in array javascripthow to add one index to every other index in the array in javascriptfind an index 3d to a value in an array and get every other value after thatjavascript get every other elementhow to get every other value in an arraychange ever other number in an array in javascriptjavascript take every other element in arrayfor loop every other javascriptget every other element in array jsget every other item in an array