Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Execute digits in even places in a JavaScript array?
In JavaScript, extracting elements from even positions in an array requires understanding that array indexing starts from 0. Elements at even positions are at indices 0, 2, 4, etc., while elements at odd positions are at indices 1, 3, 5, etc.
To extract elements from even positions, we can use various methods like filter(), traditional loops, or specific iteration patterns.
Understanding Array Positions
JavaScript arrays are zero-indexed, meaning:
- Even positions: indices 0, 2, 4, 6...
- Odd positions: indices 1, 3, 5, 7...
Using the filter() Method
The filter() method creates a new array with elements that pass a test. It doesn't modify the original array and skips empty elements.
Syntax
array.filter((element, index) => {
return condition; // return true to include element
});
Example 1: Extract Elements at Even Positions
<!DOCTYPE html>
<html>
<head>
<title>Elements at Even Positions</title>
</head>
<body>
<p id="result"></p>
<script>
var arr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
// Extract elements at even positions (indices 0, 2, 4, etc.)
let evenPositions = arr.filter((element, index) => index % 2 === 0);
document.getElementById('result').innerHTML =
'Original array: [' + arr.join(', ') + ']<br>' +
'Elements at even positions: [' + evenPositions.join(', ') + ']';
</script>
</body>
</html>
Original array: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] Elements at even positions: [1, 3, 5, 7, 9]
Using Traditional For Loop
Example 2: Loop with Condition Check
<!DOCTYPE html>
<html>
<head>
<title>Even Positions with For Loop</title>
</head>
<body>
<p id="result"></p>
<script>
var arr = [10, 20, 30, 40, 50, 60, 70, 80];
var evenPositionElements = [];
function getEvenPositions(array) {
for (var i = 0; i < array.length; i++) {
if (i % 2 === 0) { // Check if index is even
evenPositionElements.push(array[i]);
}
}
return evenPositionElements;
}
var result = getEvenPositions(arr);
document.getElementById('result').innerHTML =
'Original array: [' + arr.join(', ') + ']<br>' +
'Elements at even positions: [' + result.join(', ') + ']';
</script>
</body>
</html>
Original array: [10, 20, 30, 40, 50, 60, 70, 80] Elements at even positions: [10, 30, 50, 70]
Using Step-by-Step Iteration
Example 3: Starting from Index 0 with Step 2
<!DOCTYPE html>
<html>
<head>
<title>Step Iteration for Even Positions</title>
</head>
<body>
<p id="result"></p>
<script>
var arr = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H'];
var evenPositionElements = [];
function getEvenPositions(array) {
// Start from index 0 and increment by 2 each time
for (var i = 0; i < array.length; i += 2) {
evenPositionElements.push(array[i]);
}
return evenPositionElements;
}
var result = getEvenPositions(arr);
document.getElementById('result').innerHTML =
'Original array: [' + arr.join(', ') + ']<br>' +
'Elements at even positions: [' + result.join(', ') + ']';
</script>
</body>
</html>
Original array: [A, B, C, D, E, F, G, H] Elements at even positions: [A, C, E, G]
Comparison of Methods
| Method | Readability | Performance | Best For |
|---|---|---|---|
filter() |
High | Good | Functional programming style |
| For loop with condition | Medium | Excellent | Maximum control and performance |
| Step iteration (i += 2) | High | Excellent | Simple and efficient |
Conclusion
All three methods effectively extract elements from even positions in JavaScript arrays. The filter() method offers clean, functional syntax, while step iteration provides the most efficient approach for this specific task.
