Urara-Blog/node_modules/.pnpm-store/v3/files/1d/a881ca5005eee8016e754ec0e60a7221faa6b0dccfd67b06d4a70ab44677c0ee651b3908e648ed403040a8ff6644222286e0d9f16d36599e9e4d0c483e1489
2022-08-14 01:14:53 +08:00

61 lines
1.3 KiB
Text

---
description: 'Disallow iterating over an array with a for-in loop.'
---
> 🛑 This file is source code, not the primary documentation location! 🛑
>
> See **https://typescript-eslint.io/rules/no-for-in-array** for documentation.
This rule prohibits iterating over an array with a for-in loop.
## Rule Details
A for-in loop (`for (var k in o)`) iterates over the properties of an Object.
While it is legal to use for-in loops with array types, it is not common.
for-in will iterate over the indices of the array as strings, omitting any "holes" in
the array.
More common is to use for-of, which iterates over the values of an array.
If you want to iterate over the indices, alternatives include:
```js
array.forEach((value, index) => { ... });
for (const [index, value] of array.entries()) { ... }
for (let i = 0; i < array.length; i++) { ... }
```
Examples of code for this rule:
<!--tabs-->
### ❌ Incorrect
```js
for (const x in [3, 4, 5]) {
console.log(x);
}
```
### ✅ Correct
```js
for (const x in { a: 3, b: 4, c: 5 }) {
console.log(x);
}
```
## Options
```jsonc
// .eslintrc.json
{
"rules": {
"@typescript-eslint/no-for-in-array": "error"
}
}
```
This rule is not configurable.
## When Not To Use It
If you want to iterate through a loop using the indices in an array as strings, you can turn off this rule.