Code Inspection: Unfiltered for..in loop
Reports unfiltered for-in
loops.
Object
's prototype may be incorrectly modified. For example, the following code will print 42 and myMethod:
Object.prototype.myMethod = function myMethod() {};
let a = { foo: 42 };
for (let i in a) {
console.log(a[i]);
}
Suggests replacing the whole loop with a Object.keys()
method or adding a hasOwnProperty()
check. After applying the quick-fix the code looks as follows:
for (let i in a) {
if (a.hasOwnProperty(i)) {
console.log(a[i]);
}
}
Suppress an inspection in the editor
Position the caret at the highlighted line and press Alt+Enter or click
.
Click the arrow next to the inspection you want to suppress and select the necessary suppress action.
Last modified: 11 February 2022