Skip to content
Advertisement

Problems with loop for in ESLint

I have a problem with ESLint and I do not know what to do anymore, because without it it does not allow me to move forward. What I’m doing is concatenating the numbers that come to me, for example: [“1”, “2”] the output would be: 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 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

How could I make this valid for ESLint? I know there are other questions already but it does not work for me the next code.

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

Thank you!

Advertisement

Answer

You can refactor the first function like so:

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

Now it’ll only roll over the properties you assigned to your object rather than all the inherited stuff too.

User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement