Bladeren bron

5.6: Using arrow functions.

Frederic G. MARAND 5 jaren geleden
bovenliggende
commit
ebb923b00f
1 gewijzigde bestanden met toevoegingen van 16 en 2 verwijderingen
  1. 16 2
      app/app.ts

+ 16 - 2
app/app.ts

@@ -10,24 +10,28 @@ function GetAllBooks() {
       author: 'James Joyce',
       available: true,
       category: Category.Fiction,
+      id: 1,
       title: 'Ulysses',
     },
     {
       author: 'Ernest Hemingway',
       available: false,
       category: Category.Fiction,
+      id: 2,
       title: 'A farewall to arms',
     },
     {
       author: 'Maya Angelou',
       available: true,
       category: Category.Poetry,
+      id: 3,
       title: 'I know why the caged bird sings',
     },
     {
       author: 'Herman Melville',
       available: true,
       category: Category.Fiction,
+      id: 4,
       title: 'Moby Dick',
     }
   ];
@@ -64,11 +68,21 @@ function GetBookTitlesByCategory(categoryFilter: Category): Array<string> {
   return filteredTitles;
 }
 
+function GetBookById(id: number) {
+  const allBooks = GetAllBooks();
+  return allBooks.filter(book => book.id === id)[0];
+}
+
 function LogBookTitles(titles: string[]): void {
   for (let title of titles) {
     console.log(title);
   }
 }
 
-const poetryBooks = GetBookTitlesByCategory(Category.Poetry);
-LogBookTitles(poetryBooks);
+//******************************************************************************
+
+const fictionBookTitles = GetBookTitlesByCategory(Category.Fiction);
+// LogBookTitles(fictionBookTitles);
+fictionBookTitles.forEach((val, idx, arr) => console.log(`${++idx} ${val}.`));
+
+console.log(GetBookById(2));