Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class User {
- id: number;
- name: string;
- email: string;
- constructor(id: number, name: string, email: string) {
- this.id = id;
- this.name = name;
- this.email = email;
- }
- static getById(id: number, users: User[]): User | undefined {
- return users.find(user => user.id === id);
- }
- }
- const users = [
- new User(1, 'Alice', 'alice@example.com'),
- new User(2, 'Bob', 'bob@example.com'),
- new User(3, 'Charlie', 'charlie@example.com'),
- ];
- const user = User.getById(2, users);
- if (user) {
- console.log(user.name);
- } else {
- console.log('User not found');
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement