for-of
loop for array JS-0361for-of
loop instead of a for
loop with this simple iteration207 return false;
208 }
209 let postID = 0;
210 for(let i = 0; i < checks.length; i++) {211 if(checks[i].id.indexOf("check") === 0) {212 postID = Number.parseInt(checks[i].id.replace("check", ""));213 break;214 }215 }216 window.location.pathname = `${webroot}manage/bans?dir=${boardDir}&postid=${postID}`;
217}
218
for-of
loop instead of a for
loop with this simple iteration79$(() => {
80 let hiddenPosts = getStorageVal("hiddenposts", "").split(",");
81 if(typeof hiddenPosts === "number") hiddenPosts = [hiddenPosts];
82 for(let i = 0; i < hiddenPosts.length; i++) {83 const id = hiddenPosts[i];84 setThreadVisibility(id, false);85 setPostVisibility(id, false);86 }87});
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]);
}