Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ESLint doesn't allow for in

I have an object

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

and I use for in like this:

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: 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 should I edit my code?

like image 796
user7334203 Avatar asked May 05 '17 14:05

user7334203


3 Answers

It says,

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

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]));
})
like image 82
rishipuri Avatar answered Oct 23 '22 04:10

rishipuri


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.

like image 14
user7334203 Avatar answered Oct 23 '22 05:10

user7334203


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

var myValuesInArray = Object.values(currentValues);
like image 4
quirimmo Avatar answered Oct 23 '22 03:10

quirimmo