Skip to content

Commit 774386e

Browse files
committed
feat: completes loader and extra credit for lesson10
1 parent 0fe5109 commit 774386e

File tree

1 file changed

+18
-6
lines changed

1 file changed

+18
-6
lines changed

lesson_10/libraries/src/loaders/chanel_hutt_loader.ts

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,24 @@ export class ChanelHuttLoader implements Loader {
1111
async loadData(): Promise<MediaItem[]> {
1212
const credits = await this.loadCredits();
1313
const mediaItems = await this.loadMediaItems();
14-
15-
console.log(
16-
`Loaded ${credits.length} credits and ${mediaItems.length} media items`,
17-
);
18-
19-
return [...mediaItems.values()];
14+
// Create a Hashmap to where the key is a string(MediaItem ID) and the value is the MediaItem object.
15+
const hashMapIndex = new Map<string, MediaItem>();
16+
// Loops through the mediaItems and adds them to the map by their ID.
17+
for (const mediaItem of mediaItems) {
18+
hashMapIndex.set(mediaItem.getId(), mediaItem);
19+
}
20+
// Loops through the credits and adds them to the mediaItem by getting the mediaItem ID.
21+
for (const credit of credits) {
22+
const mediaItem = hashMapIndex.get(credit.getMediaItemId());
23+
if (mediaItem) {
24+
mediaItem.addCredit(credit);
25+
}
26+
console.log(
27+
`Loaded ${credits.length} credits and ${mediaItems.length} media items`,
28+
);
29+
}
30+
// Returns an array of the values from the hashmap.
31+
return Array.from(hashMapIndex.values());
2032
}
2133

2234
async loadMediaItems(): Promise<MediaItem[]> {

0 commit comments

Comments
 (0)