Advertisement
Gygaweb

functional proggramming example

Feb 18th, 2023
596
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
TypeScript 0.49 KB | Software | 0 0
  1. interface User {
  2.   id: number;
  3.   name: string;
  4.   email: string;
  5. }
  6.  
  7. const users: User[] = [
  8.   { id: 1, name: 'Alice', email: 'alice@example.com' },
  9.   { id: 2, name: 'Bob', email: 'bob@example.com' },
  10.   { id: 3, name: 'Charlie', email: 'charlie@example.com' },
  11. ];
  12.  
  13. function getUserById(id: number): User | undefined {
  14.   return users.find(user => user.id === id);
  15. }
  16.  
  17. const user = getUserById(2);
  18. if (user) {
  19.   console.log(user.name);
  20. } else {
  21.   console.log('User not found');
  22. }
  23.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement