Home > Software design >  Alternatives for Using 'ForInStatement' is not allowed
Alternatives for Using 'ForInStatement' is not allowed

Time:07-27

I have this fragment of code in my Apps Script App to iterate from the list array.

let list = [[name1, id1, sheet1], [name2, id2, sheet2], [name3, id3, sheet3]];
for (let n in list) {
  let txtFileId = list[n][1];
  let txtFileSheet = list[n][2];
  let txt_file = DriveApp.getFileById(txtFileId);
};

It works well, but ESLINT gives me this pair of errors. I don't understand why eslint doesn't like for in...

  • Using 'ForInStatement' is not allowed (.eslint no-restricted-syntax)
  • The body of a for-in should be wrapped in an if statement to filter unwanted properties from the prototype (.eslint guard-for-in)

I've tried other kind of loops but this is the only one that I can make work (have to say I'm very new), so.. which would be the best (and fastest) way to iterate though this array?

I've simplified the code, this loop make a lot of actions for each element that's why I said fastest.

CodePudding user response:

Try using for...of with destructuring assignment:

let list = [[name1, id1, sheet1], [name2, id2, sheet2], [name3, id3, sheet3]];

for (const [name, txtFileId, txtFileSheet] of list) {
  let txt_file = DriveApp.getFileById(txtFileId);
};

CodePudding user response:

I dont know why people use ESLINT or other plugins, life is so much simpler with VIM editor.

Anyways, try using forEach loop it works well with all plugins.

  • Related