for-of
loop for array JS-0361for-of
loop instead of a for
loop with this simple iteration31 return (_params: URLSearchParams): T[] => {
32 const params: string[] = _params.get(key)?.split(',') || [];
33 const values: T[] = [];
34 for (let i = 0; i < params.length; i++) {35 const param = params[i];36 if (filter(param)) {37 values.push(param as T);38 }39 }40 if (values.length) {
41 return values;
42 }
for-of
loop instead of a for
loop with this simple iteration26 return (urlSearchParams: URLSearchParams): T[] => {
27 const params: string[] = urlSearchParams.get(key)?.split(',') || [];
28 const values: T[] = [];
29 for (let i = 0; i < params.length; i++) {30 const param = params[i];31 if (filter(param)) {32 values.push(param as T);33 }34 }35 if (values.length) {
36 return values;
37 }
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]);
}