0

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.

Adrian Mole
  • 49,934
  • 160
  • 51
  • 83
alsanmph
  • 77
  • 5

2 Answers2

1

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);
};
TheAddonDepot
  • 8,408
  • 2
  • 20
  • 30
0

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.

  • You can use eslint with vim. And there's a [reason](https://stackoverflow.com/questions/500504/why-is-using-for-in-for-array-iteration-a-bad-idea) why the linter throws the error. – TheMaster Jul 27 '22 at 09:32