for-of
loop for array JS-0361for-of
loop instead of a for
loop with this simple iteration74 public triggerReset = async () => {
75 if (process.env.NODE_ENV === 'test') {
76 const reqKeys = await this.nosql.keys('req:*')
77 for (let i = 0; i < reqKeys.length; i++) {78 await this.nosql.del(reqKeys[i])79 }80 }
81 }
82}
for-of
loop instead of a for
loop with this simple iteration30 public triggerReset = async () => {
31 if (process.env.NODE_ENV === 'test') {
32 const banKeys = await this.nosql.keys('ban:*')
33 for (let i = 0; i < banKeys.length; i++) {34 await this.nosql.del(banKeys[i])35 }36 }
37 }
38}
A for-of
loop is recommended when the loop index is only used to read from the collection.
for (let i = 0; i < arr.length; i++) {
console.log(arr[i]);
}
for (const x of arr) {
console.log(x);
}
for (let i = 0; i < arr.length; i++) {
// i is used to write to arr, so for-of could not be used.
arr[i] = 0;
}
for (let i = 0; i < arr.length; i++) {
// i is used independent of arr, so for-of could not be used.
console.log(i, arr[i]);
}