在JavaScript或TypeScript中,for...of和for...in是两种不同的循环语法,用于遍历数组或对象的属性。它们之间有以下区别:
for...of循环: 用于遍历可迭代对象(例如数组、字符串、Set、Map等)的值。 在每次迭代中,将迭代的值赋给声明的变量(例如const logoutUrl of logoutUrlList中的logoutUrl)。 适用于遍历数组的元素。 for...in循环: 用于遍历对象的可枚举属性(包括自身的属性和继承的属性)。 在每次迭代中,将迭代的属性键赋给声明的变量(例如const logoutUrl in logoutUrlList中的logoutUrl)。 适用于遍历对象的属性。
const logoutUrlList: string[] = [
"/refreshToken/login",
"/judgeHasNotificationEvent",
"/info",
"/listMenuByUser",
]
for (const logoutUrl of logoutUrlList) {
if (url.includes(logoutUrl)) {
isContainUrl = true
break
}
}
/refreshToken/login
/judgeHasNotificationEvent
/info
/listMenuByUser
const logoutUrlList: string[] = [
"/refreshToken/login",
"/judgeHasNotificationEvent",
"/info",
"/listMenuByUser",
]
for (const logoutUrl in logoutUrlList) {
// if (url.includes(logoutUrl)) {
// isContainUrl = true
// break
// }
console.log(logoutUrl)
}
0
1
2
3