Array.prototype.entries()
entries() Returns:
Iterator · Updated March 13, 2026 · Array Methods array entries iteration
The entries() method returns a new array iterator that contains the key-value pairs for each index in the array. This allows you to iterate over both the index and the value of each element in a single pass.
Syntax
array.entries()
Parameters
None. This method takes no parameters.
Examples
Basic usage with iterator
const fruits = ['apple', 'banana', 'cherry'];
const iterator = fruits.entries();
console.log(iterator.next().value);
// [0, 'apple']
console.log(iterator.next().value);
// [1, 'banana']
console.log(iterator.next().value);
// [2, 'cherry']
Using for…of loop
const colors = ['red', 'green', 'blue'];
for (const [index, value] of colors.entries()) {
console.log(`Index ${index}: ${value}`);
}
// Index 0: red
// Index 1: green
// Index 2: blue
Converting to an array of pairs
const nums = [10, 20, 30];
const pairs = [...nums.entries()];
console.log(pairs);
// [[0, 10], [1, 20], [2, 30]]
Finding index with value condition
const data = [5, 12, 8, 130, 44];
for (const [index, value] of data.entries()) {
if (value > 100) {
console.log(`Found ${value} at index ${index}`);
break;
}
}
// Found 130 at index 3
Filtering by index
const items = ['a', 'b', 'c', 'd', 'e'];
// Get items at odd indices
const oddItems = [...items.entries()]
.filter(([index]) => index % 2 === 1)
.map(([, value]) => value);
console.log(oddItems); // ['b', 'd']
Transforming with index awareness
const numbers = [1, 2, 3, 4, 5];
// Multiply each number by its index
const indexed = [...numbers.entries()].map(([i, n]) => n * i);
console.log(indexed); // [0, 2, 6, 12, 20]
Practical: finding duplicate indices
const arr = [1, 2, 3, 2, 4, 3, 5];
const seen = new Set();
for (const [index, value] of arr.entries()) {
if (seen.has(value)) {
console.log(`Duplicate ${value} at index ${index}`);
} else {
seen.add(value);
}
}
// Duplicate 2 at index 3
// Duplicate 3 at index 5
Object creation from entries
const keys = ['name', 'age', 'city'];
const values = ['Alice', 30, 'NYC'];
const obj = Object.fromEntries(keys.entries().map(([i, k], _, arr) =>
[k, values[i]]
));
console.log(obj); // { name: 'Alice', age: 30, city: 'NYC' }
Notes
- The returned iterator yields
[index, value]pairs - Destructuring in
for...of(const [index, value]) is the cleanest pattern - Iterator can be converted to array with spread operator
See Also
- array::keys — iterate over indices only
- array::values — iterate over values only