ESLint中的循环问题

问题描述 投票:0回答:2

我有一个ESLint的问题,我不知道该怎么做,因为没有它它不允许我继续前进。我正在做的是连接到我的数字,例如:[“1”,“2”]输出将是:12

    let _sortItem = '';
    for (var p in this.state.sortItem2) {
      _sortItem += this.state.sortItem2[p];
    }
    this._month = '';
    for (var m in this.state.sortItem1) {
      this._month += this.state.sortItem1[m];
    }

ESLint:for..in循环遍历整个原型链,这几乎不是你想要的。使用Object。{keys,values,entries},并迭代生成的数组。 (没有限制的语法

我怎么能让这对ESLint有效?我知道还有其他问题,但它对我下一个代码不起作用。

Object.keys( this.state.sortItem2).forEach(function(p) {
  yield put(setCurrentValue(p, currentValues[p]));
})

谢谢!

javascript
2个回答
1
投票

您可以像这样重构第一个函数:

Object.keys(this.state.sortItem2).forEach(key => {
 _sortItem += this.state.sortItem2[key]
})

现在它只会滚动您分配给对象的属性,而不是所有继承的东西。


0
投票

接受的答案很好,但是当你真正对这些值感兴趣时,使用Object.keys并不是最好的解决方案:

Object.values(this.state.sortItem2).forEach(value => {
  _sortItem += value;
});

或者,与for .. of

for (let value of Object.values(this.state.sortItem2)) {
  _sortItem += value;
}

或者,更简单:

const _sortItem = Object.values(this.state.sortItem2).join('');
© www.soinside.com 2019 - 2024. All rights reserved.