Javascript ESLint 不允许 in

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/43807515/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 02:10:24  来源:igfitidea点击:

ESLint doesn't allow for in

javascripteslintfor-in-loop

提问by user7334203

I have an object

我有一个对象

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

and I use for in like this:

我像这样使用 for:

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

ESLint shows me an error which is saying:

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

ESLint: for..in 循环遍历整个原型链,这实际上永远不是你想要的。使用 Object.{keys,values,entries},并迭代结果数组。(无限制语法

How should I edit my code?

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

回答by rishipuri

It says,

它说,

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

使用 Object.{keys,values,entries},并迭代结果数组。

So you could do something like this to get the object keys as an array and then loop through the keys to make necessary changes.

所以你可以做这样的事情来获取对象键作为数组,然后遍历键进行必要的更改。

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

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

回答by user7334203

I used the following:

我使用了以下内容:

const keys = Object.keys(currentValues);
const values = Object.values(currentValues);
for (let i = 0; i < keys.length; i += 1) {
    yield put(setCurrentValue(keys[i], values[i]));
}

This is correct and without ESLint errors.

这是正确的,没有 ESLint 错误。

回答by quirimmo

You can get the array of all your values inside your object just doing

您可以在对象中获取所有值的数组

var myValuesInArray = Object.values(currentValues);

回答by Daher

try this instead:

试试这个:

Object.keys(currentValues).map(key => (yield put(setCurrentValue(key, currentValues[key]))));

回答by Andres Felipe

I know it is similar to the above, but here is a full example:

我知道它与上面的类似,但这里有一个完整的例子:

const data = res.data;
const keys = Object.keys(data);
const values = Object.values(data);

for (let i = 0; i <= keys.length; i += 1) {
  if (Object.prototype.hasOwnProperty.call(values, i)) {
     this.rows.push({
        name: values[i].name,
        email: values[i].email,
        address: values[i].address,
        phone: values[i].phone,
        role: values[i].role,
  });
 }
}

回答by jakow

Using for...inwill iterate over all properties including those from Object prototype. I am not sure why you are doing Object.prototype.hasOwnProperty.call(currentValues, key)instead of just: currentValues.hasOwnProperty(key). I think this should make ESLint aware that you are filtering for own properties only.

Usingfor...in将迭代所有属性,包括来自 Object 原型的属性。我不确定您为什么要这样做,Object.prototype.hasOwnProperty.call(currentValues, key)而不仅仅是: currentValues.hasOwnProperty(key)。我认为这应该让 ESLint 意识到您只过滤自己的属性。

However, I suggest using for (const key of Object.keys()), which is more semantic.

但是,我建议使用for (const key of Object.keys()),它更具语义。