Wrap the body of a for-in loop in an if statement with a hasOwnProperty guard
565 const d = c.commits[0].userData;
566
567 // eslint-disable-next-line guard-for-in
568 for (const key in d) {569 if (!(key in uData)) {570 identical = false;571 break;572 }573 if (d[key] !== uData[key]) {574 identical = false;575 break;576 }577578 identical = true;579 }580 }
581
582 t.is(true, identical);
Wrap the body of a for-in loop in an if statement with a hasOwnProperty guard
683 const d = c.refs[1].userData;
684
685 // eslint-disable-next-line guard-for-in
686 for (const key in d) {687 if (!(key in uData)) {688 identical = false;689 break;690 }691 if (d[key] !== uData[key]) {692 identical = false;693 break;694 }695696 identical = true;697 }698 }
699
700 t.is(true, identical);
Wrap the body of a for-in loop in an if statement with a hasOwnProperty guard
626 process.stdout.write('User Data:');
627 let seperator = ' ';
628 // eslint-disable-next-line guard-for-in
629 for (const key in commit.userData) {630 if ({}.hasOwnProperty.call(commit.userData, key)) {631 process.stdout.write(`${seperator}${key}=${commit.userData[key]}`);632 }633 seperator = ', ';634 }635 process.stdout.write('\n');
636 }
637 process.stdout.write(`\n ${commit.message}\n\n\n`);
Description
Looping over objects with a for in
loop will include properties that are inherited through the prototype chain.
This behavior can lead to unexpected keys in your for loop.
Bad Practice
for (const key in foo) {
doSomething(key);
}
Recommended
for (const key in foo) {
if (Object.prototype.hasOwnProperty.call(foo, key)) {
doSomething(key);
}
}
for (const key of Object.keys(foo)) {
doSomething(key);
}