ts for循环示例
ts
interface IUser {
name: string;
age: number;
}
const user: IUser = {
name: 'John',
age: 30,
};
// 将对象转换为键值对数组,然后使用 for...of 遍历
for (const [key, value] of Object.entries(user)) {
console.log(key, value);
}
// 使用 for...in 遍历对象的属性
for (let _key in user) {
const key = _key as keyof IUser;
console.log(key, user[key]);
}