type Subject = {
id: number;
title: string;
};
type Role = "TEACHER" | "STUDENT";
type User = {
id: number;
title: string;
age: number;
subjects: Subject[];
role?: Role;
};
const users: User[] = [
{ id: 1, title: "t1", age: 12, subjects: [{ id: 1, title: "12" }], role:"STUDENT" },
{ id: 8, title: "t2", age: 22, subjects: [{ id: 2, title: "22" }] , role:"TEACHER"},
{ id: 3, title: "t3", age: 32, subjects: [{ id: 3, title: "32" }] , role:"STUDENT"},
{ id: 4, title: "t3", age: 32, subjects: [{ id: 3, title: "32" }] , role:"TEACHER"},
{ id: 5, title: "t3", age: 32, subjects: [{ id: 3, title: "32" }] , role:"STUDENT"},
{ id: 6, title: "t3", age: 32, subjects: [{ id: 3, title: "32" }] , role:"STUDENT"},
{ id: 7, title: "t3", age: 32, subjects: [{ id: 3, title: "32" }] , role:"TEACHER"},
{ id: 2, title: "t3", age: 32, subjects: [{ id: 3, title: "32" }] , role:"STUDENT"},
{ id: 9, title: "t3", age: 32, subjects: [{ id: 3, title: "32" }] , role:"STUDENT"},
{ id: 10, title: "t3", age: 32, subjects: [{ id: 3, title: "32" }] , role:"TEACHER"},
];
// type College = {
// id: number;
// title: string;
// students: User[];
// teachers: User[];
// };
// const college: College[] = [
// {
// id: 1,
// title: "t1",
// students: [{ id: 1, title: "t1", age: 12, subjects:users.filter(item => item.role == "STUDENT") }],
// teachers: [{ id: 2, title: "t2", age: 22, subjects:users.filter(item => item.role == "TEACHER") }],
// },
// ];
// console.log(college);
class College{
id:number;
title:string;
students: User[] = [];
teachers :User[]=[];
constructor(id:number,title:string){
this.id = id;
this.title = title;
}
findUserById(id:number) : User | null{
users.find((item => item.id==id));
return users[id];
}
addStudent(user:User){
this.students.push(user);
}
removeUserById(id:number):void{
const index = users.findIndex(item => item.id == id);
users.splice(index, 1)
}
clear():void {
users.splice(0);
}
}
const example = new College(564893, "example");
console.log(example.findUserById(1));
console.table(example.removeUserById(2));
console.table(users);
example.clear();
-----------------