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]

4.25
8

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

4.25 (8 Votes)
0
3.67
3
Parisa 115 points

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

3.67 (3 Votes)
0
Are there any code examples left?
Create a Free Account
Unlock the power of data and AI by diving into Python, ChatGPT, SQL, Power BI, and beyond.
Sign up
Develop soft skills on BrainApps
Complete the IQ Test
Relative searches
how to get every other value in an array javascript array get every other element every other item in array javascript javascript include every second element in array javascript every other element in array get every other element in list javascript get every other item from array javascript join array every other element js add every other number in array change ever other number in an array in JavaScript how to perform operation on every other value in array js for loop every other javascript javascript insert object in arrayevery other 3rd javsctip insert object every other 3rd javsctip insert object every other loop every other number in array javascript slice out every 2nd item in the array js every other umber in a js array javascript slice every other how to access every other index in an array get every other element in array js how to add an index to every other index in the array in javascript how to add one index to every other index in the array in javascript for loop every other js find an index = to a value in an array and get every other value after that how to filter every other element in an array with javascript javascript take every other element in array select every other wtring in js how to map through every other object in an array javascript get every other item from list into new array javascript javascript get every other element how to look at every other item in an array for loop how to look at every other item in an array slice every other element js get every other item in an array
Made with love
This website uses cookies to make IQCode work for you. By using this site, you agree to our cookie policy

Welcome Back!

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign in
Recover lost password
Or log in with

Create a Free Account

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign up
Or sign up with
By signing up, you agree to the Terms and Conditions and Privacy Policy. You also agree to receive product-related marketing emails from IQCode, which you can unsubscribe from at any time.
Creating a new code example
Code snippet title
Source