我通常遵循 Airbnb 的 ESLint 配置,我注意到我在使用 For...In 循环时遇到了错误?为什么会这样?
我在这里读了一点,但我想要更详细的解释或示例。
const data = {
color : 'blue',
movies : 'action',
hobby : 'football',
};
for (let prop in data) {
console.log(`prop: ${prop} and value is ${data[prop]}`);
}
//Throws Guarding for in, should be wrapped in an if statement to
//filter unwated properties from the prototype.
Object.keys(data).forEach((element) => {
console.log(`prop: ${element} and value is ${data[element]}`);
});
//This is Okay