eslint error "guard-for-in" not clear how to work with for-in
Asked Answered
S

4

9

I'm working with cucumber js and I want to fill out some fields in an application, so i'm using a for-in to get the data from the rowHash but i'm getting the error message "The body of a for-in should be wrapped in an if statement to filter unwanted properties from the prototype guard-for-in" i'm not sure how I should code my for-in with the if inside the for.

this is my code:

this.fillRequiredfields = function(dataTable){
  var rows = dataTable.rowsHash();
  for (var row in rows) {
    var val = rows[row];

    if (row === 'firstname') {
      element(by.name('firstName')).sendKeys(val).isPresent();
    }
    if (row === 'lastname') {
      element(by.name('lastName')).sendKeys(val).isPresent();
    }
    if (row === 'emailaddress') {
      element(by.name('emailAddress')).sendKeys(val).isPresent();
    }
    if (row === 'displayname') {
      element(by.name('displayName')).sendKeys(val).isPresent();
    }
    if (row === 'password') {
      element(by.name('newPassword')).sendKeys(val).isPresent();
    }
  }
};

So when I try to do the commit in git i'm getting the "guard-for-in" from eslint. If somebody can explain me how I should do the if in the for-in that could be good.

Hope you can help me.

Sewn answered 11/1, 2018 at 20:14 Comment(2)
The eslint documentation for this rule has an example of correct usage.Leighannleighland
Yeah but i don't get what is mean the " if (Object.prototype.hasOwnProperty.call(foo, key)) { doSomething(key); }"Sewn
S
12

I already solve this, in my case the solution was:

instead of

var val = rows[row];

I add the if with the hasOwnProperty(), like this:

if (rows.hasOwnProperty(row))

So the code is like this:

for (var row in rows) {
  if (rows.hasOwnProperty(row)){

    if (row === 'firstname') {
      element(by.name('firstName')).sendKeys(rows[row]).isPresent();
    }
    if (row === 'lastname') {
      element(by.name('lastName')).sendKeys(rows[row]).isPresent();
    }
    if (row === 'emailaddress') {
      element(by.name('emailAddress')).sendKeys(rows[row]).isPresent();
    }
    if (row === 'displayname') {
      element(by.name('displayName')).sendKeys(rows[row]).isPresent();
    }
    if (row === 'password') {
      element(by.name('newPassword')).sendKeys(rows[row]).isPresent();
    }
  }
}

Hope can help to somebody else.

Sewn answered 17/1, 2018 at 18:18 Comment(0)
C
5

In some rare cases

if (rows.hasOwnProperty(row))

might trigger undesired effect (for example if this is a function hasOwnProperty()). So this is why suggested method is

Object.prototype.hasOwnProperty.call(rows, row)
Chancy answered 7/5, 2019 at 13:47 Comment(0)
S
3

You can also use the for...of statement (https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/for...of) so your code would be something like:

for (var row of Object.keys(rows)) {
  if (row === 'firstname') {
    element(by.name('firstName')).sendKeys(rows[row]).isPresent();
  }
  [...]
}
Sylvanus answered 3/2, 2021 at 14:46 Comment(0)
P
0

Here is another variation of getting around guard-for-in using an array method.

Object.entries(rows).forEach(([key, value]) => {
  if (value === 'firstname') {
    element(by.name('firstName')).sendKeys(val).isPresent();
  }
  // ...
});
Parris answered 22/2, 2023 at 1:9 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.