for-of
loop for array JS-0361for-of
loop instead of a for
loop with this simple iteration276 );
277
278 const files = osWalk(KNOBS_DIR);
279 for (let i = 0; i < files.length; i++) {280 const file = files[i];281282 // Get the knob name file parts without the id to check if the knob already exists.283 const knobFileName = knobFile.filename.split("_").slice(0, 3).join("_");284285 if (path.basename(file).startsWith(knobFileName)) {286 vscode.window.showErrorMessage("Knob already exists");287 return;288 }289 }290
291 fs.writeFileSync(knobFile.path, "");
292 vscode.window.showTextDocument(vscode.Uri.file(knobFile.path), { preview: false });
for-of
loop instead of a for
loop with this simple iteration206
207 // TODO: send all the code at once and then rename the files.
208
209 for (let i = 0; i < files.length; i++) {210 const file = files[i];211 if (!file.endsWith(".py")) {212 continue;213 }214215 const knobFile = new KnobFile(file);216 const result = await sendToNuke(syncNodeSnippet(knobFile));217 if (result.error) {218 vscode.window.showErrorMessage(`Failed to sync ${file}: ${result.errorMessage}`);219 continue;220 }221 const newName = knobFile.newName(result.message);222223 try {224 fs.renameSync(file, path.join(KNOBS_DIR, newName));225 } catch (error) {226 vscode.window.showErrorMessage(`Failed to rename ${file} to ${newName}: ${error}`);227 }228229 sleep(1000);230 }231
232 this.refresh();
233 }
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]);
}