javascript - ESLint 不允许 in

标签 javascript eslint for-in-loop

我有一个对象

currentValues= {hey:1212, git:1212, nmo:12121}

我这样使用 for :

for (const key in currentValues) {
    if (Object.prototype.hasOwnProperty.call(currentValues, key)) {
        yield put(setCurrentValue(key, currentValues[key]));
    }
}

ESLint 向我显示了一个错误,内容是:

ESLint: for..in loops iterate over the entire prototype chain, which is virtually never what you want. Use Object.{keys,values,entries}, and iterate over the resulting array. (no-restricted-syntax

我应该如何编辑我的代码?

最佳答案

它说,

Use Object.{keys,values,entries}, and iterate over the resulting array.

因此,您可以执行类似的操作来获取对象键作为数组,然后循环遍历键以进行必要的更改。

currentValues= {hey:1212, git:1212, nmo:12121}

Object.keys(currentValues).forEach(function(key) {
  yield put(setCurrentValue(key, currentValues[key]));
})

关于javascript - ESLint 不允许 in,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43807515/

相关文章:

javascript - 如何在 ES6 中生成不重复数字的数组?

.net - 如何在页面加载前执行 Javascript 函数?

javascript - HTML/JavaScript - 数量的向上和向下按钮不起作用

angular - ESLint 就像 TSLint 中的全局变量

javascript - Javascript 的 "for in"构造是否应该迭代 length 属性?

javascript - 当新数组包含现有数组时,无法通过拼接删除单元格

javascript - 仅对新文件执行 eslint 规则

javascript - 如何在 JavaScript 文件中允许空格?

Python for-in-loop 停止迭代从 for-in-loop 创建的列表对象

javascript - 如何在 for...in 中获取嵌套对象属性的正确属性?