@putout/plugin-convert-for-in-to-for-of

2.0.1 • Public • Published

@putout/plugin-convert-for-in-to-for-of NPM version

The for...in statement iterates over all enumerable properties of an object that are keyed by strings.

The for...of statement creates a loop which invokes a custom iteration hook with statements to be executed for the value of each element of an array.

(c) MDN

🐊Putout plugin adds ability to convert for...in to for...of loop. Merged to @putout/plugin-for-of.

Install

npm i @putout/plugin-convert-for-in-to-for-of -D

Rule

{
    "rules": {
        "convert-for-in-to-for-of/positive": "on",
        "convert-for-in-to-for-of/negative": "on"
    }
}

Example of incorrect code

for (const item in object) {
    if (object.hasOwnProperty(item)) {
        log(item);
    }
}

for (const item in object) {
    if (!object.hasOwnProperty(item))
        continue;
    
    log(item);
}

Example of correct code

for (const item of Object.keys(object)) {
    log(item);
}

License

MIT

Package Sidebar

Install

npm i @putout/plugin-convert-for-in-to-for-of

Weekly Downloads

15,304

Version

2.0.1

License

MIT

Unpacked Size

6.09 kB

Total Files

6

Last publish

Collaborators

  • coderaiser