javascript every other element in array

Solutions on MaxInterview for javascript every other element in array by the best coders in the world

showing results for - "javascript every other element in array"
Bronte
15 Jan 2021
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]
Thiago
23 May 2019
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
Isaac
18 Jan 2021
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
queries leading to this page
for loop every other jsjavascript take every other element in arrayhow to map through every other object in an array javascriptslice every other element jsjavascript every other element in arrayget every other element in array jsjavascript slice every otherhow to look at every other item in an arrayfind an index 3d to a value in an array and get every other value after thatjavascript every other elementaccessing every other element in array javascriptjavsctip insert object every otherfor loop every other javascripthow to find every other el in the array jsevery other item in array javascriptget every other item from list into new array javascripthow to add one index to every other index in the array in javascripthow to look at every other item in an array for loopjavsctip insert object every other 3rdget every other element in list javascriptloop every other number in array javascripthow to get every other value in an arrayhow to add an index to every other index in the array in javascripthow to perform operation on every other value in array jschange ever other number in an array in javascriptjavascript insert object in arrayevery other 3rdjs add every other number in arrayselect every other wtring in jsevery other umber in a js arrayhow to filter every other element in an array with javascriptget every other item from arrayget every other item in an arrayjavascript include every second element in arrayslice out every 2nd item in the array jsjavascript get every other elementhow to access every other index in an arrayevery item of one array should match another array javascriptjavascript join array every other elementjavascript array get every other elementjavascript every other element in array