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 :: add cloudinary to gatsby javascript 
Javascript :: phaser game height 
Javascript :: isogram 
Javascript :: how to use ternary operatiion in sequelize join statement 
Javascript :: json url data is not showing in console using jquery 
Javascript :: javascript factor chain 
Javascript :: javascript canvas clip rectangle 
Javascript :: creating large element in js 
Javascript :: can you wrap redux provider within react.strictmode 
Javascript :: var x=21; var myFunction = function(){ console.log(x); var x= 20; }; myFunction(); 
Javascript :: how to avoid inheritance in angular 
Javascript :: react native anination 2 valuse 
Javascript :: how to create response time router node js 
Javascript :: svn node remains in conflict 
Javascript :: how to use getBackgroundPage 
Javascript :: when chrome loads data it resets scroll 
Javascript :: chai expect array without order 
Javascript :: keyboard is underlined in eclipse javascript 
Javascript :: how to new tab disable after hit enter in javascript 
Javascript :: error message remove after checkbox fill in jquery 
Javascript :: alaa 201 exam 
Javascript :: nodejs multipart/x-mixed-replace; boundary=BoundaryString 
Javascript :: react native map array of objects 
Javascript :: open lightbox in backend 
Javascript :: js set visibility on timeout 
Javascript :: add padding to a div slow jquery 
Javascript :: chroma js 
Javascript :: js null vs undefine 
Javascript :: time calculate midpoint between two dates js 
Javascript :: javascripte 
ADD CONTENT
Topic
Content
Source link
Name
2+5 =