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