【问题标题】:ESLint doesn't allow for inESLint 不允许在
【发布时间】:2017-10-04 02:05:34
【问题描述】:

我有一个对象

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

我使用 for in 是这样的:

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

ESLint 向我显示一个错误:

ESLint:for..in 循环遍历整个原型链,这几乎不是您想要的。使用 Object.{keys,values,entries},并遍历结果数组。 (无限制语法

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

【问题讨论】:

  • 如果您只需要密钥,请尝试for(const key of currentValues.keys())。如果需要键和值,可以使用entries
  • @AndrewLi 你在说Object.keys()吗?如果是这样,它仍然值得怀疑,因为使用 for ... in 遍历数组是不受欢迎的。
  • @Pointy 我正在使用for...of?
  • 糟糕,抱歉,继续 :) 但仍然;该对象上没有 .keys() 函数。
  • 我没有看到数组。

标签: javascript eslint for-in-loop


【解决方案1】:

我会通过以下方式进行重构。

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

Object.keys(currentValues).forEach((e) => console.log(`${e} : ${currentValues[e]}`));

结果:

嘿:1212 混帐:1212 纳米:12121

Object.values(currentValues).forEach((e) => console.log(`Values: ${e}`));

结果:

(2)值:1212 值:12121

Object.entries(currentValues).forEach((e) => console.log(`${e[0]} : ${e[1]}`));

结果:

嘿:1212 混帐:1212 纳米:12121

【讨论】:

    【解决方案2】:

    试试这个:

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

    【讨论】:

      【解决方案3】:

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

      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,
        });
       }
      }
      

      【讨论】:

      【解决方案4】:

      我使用了以下内容:

      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]));
      }
      

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

      【讨论】:

        【解决方案5】:

        它说,

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

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

        currentValues= {hey:1212, git:1212, nmo:12121}
        
        Object.keys(currentValues).forEach(function(key) {
          yield put(setCurrentValue(key, currentValues[key]));
        })

        【讨论】:

        • 是的,但我不明白为什么?真的有问题吗?
        • 这怎么能比普通的 for in 更直观或更简短?
        • @Jonny for..in 将按照 ESLint 消息的说明遍历整个原型链,因此您最终会在循环中得到意想不到的项目。为了避免这种情况,人们会在for..in 循环中添加一个if (Object.prototype.hasOwnProperty.call...)(参见OP 代码示例)。使用Object.keys 消除了在for..in 中对if 的需要,它比使用for..in 更简洁明了。
        • “yield”表达式只允许在生成器主体中使用
        【解决方案6】:

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

        var myValuesInArray = Object.values(currentValues);
        

        【讨论】:

          【解决方案7】:

          使用for...in 将遍历所有属性,包括来自对象原型的属性。我不知道你为什么要这样做Object.prototype.hasOwnProperty.call(currentValues, key) 而不仅仅是: currentValues.hasOwnProperty(key)。 我认为这应该让 ESLint 意识到您只过滤自己的属性。

          不过,我建议使用for (const key of Object.keys()),这样更符合语义。

          【讨论】:

          猜你喜欢
          • 2020-05-19
          • 2017-10-24
          • 1970-01-01
          • 2022-01-05
          • 2020-01-06
          • 2017-03-20
          • 2018-04-07
          • 2021-10-21
          相关资源
          最近更新 更多