Skip to content
the poor coder

javascript grab only even array index

Poor Man
Published
Reading time
2 min read
On this page

Javascript: Grabbing only Even Array Index#

When it comes to working with arrays in Javascript, it's important to know how to manipulate the data in the array. One common task is to only grab the even array index. In this PAA, we'll discuss the different ways to achieve this.

Method 1: Using a for loop#

The first method is using a for loop to loop through the array and only grab the even index values. Here's an example of how to do it:

Text

const array = [1, 2, 3, 4, 5, 6];
const evenIndexArray = [];

for (let i = 0; i < array.length; i += 2) {
    evenIndexArray.push(array[i]);
}

console.log(evenIndexArray); // [1, 3, 5]

In this code, we first define our array with values ranging from 1 to 6. We then create a new empty array called evenIndexArray. We then use a for loop to loop through the original array and only grab the even index values by using i += 2 in our for loop declaration. We then push each even index value into our new array. Finally, we log the new array to the console.

Method 2: Using the filter method#

The second method is using the filter() method to filter out the even index values. Here's an example of how to do it:

Text

const array = [1, 2, 3, 4, 5, 6];
const evenIndexArray = array.filter((value, index) => {
    return index % 2 === 0;
});

console.log(evenIndexArray); // [1, 3, 5]

In this code, we first define our array with values ranging from 1 to 6. We then use the filter() method on the array to only keep the values where the index is even by checking if index % 2 === 0. We then assign the new filtered array to a variable called evenIndexArray. Finally, we log the new array to the console.

Method 3: Using the reduce method#

The third method is using the reduce() method to only keep the even index values. Here's an example of how to do it:

Text

const array = [1, 2, 3, 4, 5, 6];
const evenIndexArray = array.reduce((accumulator, currentValue, index) => {
    if (index % 2 === 0) {
        accumulator.push(currentValue);
    }
    return accumulator;
}, []);

console.log(evenIndexArray); // [1, 3, 5]

In this code, we first define our array with values ranging from 1 to 6. We then use the reduce() method on the array to only keep the values where the index is even by checking if index % 2 === 0. We then assign the new filtered array to a variable called evenIndexArray. Finally, we log the new array to the console.

These are three methods you can use to grab only even index values in a Javascript array. Choose the one that fits your scenario best and happy coding!

Keep going

2023

how to sort an array

How to Sort an Array If you have an array and you want to order its elements in a specific way, you need to use a sorting algorithm. There are several sorting algorithms available, but two of the most commonly used are…

javascript2 min read
2023

jquery mobile or desktop

jQuery Mobile or Desktop? As a web developer, I have had some experience with both jQuery Mobile and jQuery Desktop. Both frameworks have their pros and cons, and which one to use really depends on the specific project…

javascript1 min read
2023

node.js folder structure

Node.js Folder Structure If you are new to Node.js, it can be daunting to create a folder structure that will work for your project. The structure you choose will depend on the size and complexity of your project. Here…

javascript1 min read
ESC