Skip to content

Latest commit

 

History

History
51 lines (37 loc) · 1.78 KB

File metadata and controls

51 lines (37 loc) · 1.78 KB

prefer-array-iterable-methods

📝 Prefer iterating an array directly or with Array#keys() over Array#entries() when the index or value is unused.

💼🚫 This rule is enabled in the ✅ recommended config. This rule is disabled in the ☑️ unopinionated config.

🔧 This rule is automatically fixable by the --fix CLI option.

Array#entries() yields [index, value] pairs. When a for…of loop discards one half of the pair, the .entries() call is unnecessary overhead and obscures intent.

When the index is unused, iterate the array directly. When the value is unused, use Array#keys().

This rule intentionally only reports for…of loops, and only when the receiver is known to be an array. The transformation is unsafe for other built-ins, for example Map, whose .entries() and direct iteration both yield [key, value].

Without TypeScript type information, only arrays recognized through syntax (an array literal, Array()/new Array(), Array.from()/Array.of()) or a simple type annotation are reported.

Examples

// ❌
for (const [, value] of array.entries()) {
	foo(value);
}

// ✅
for (const value of array) {
	foo(value);
}
// ❌
for (const [index] of array.entries()) {
	foo(index);
}

// ✅
for (const index of array.keys()) {
	foo(index);
}
// ✅
for (const [index, value] of array.entries()) {
	foo(index, value);
}