File tree Expand file tree Collapse file tree 1 file changed +18
-6
lines changed
lesson_10/libraries/src/loaders Expand file tree Collapse file tree 1 file changed +18
-6
lines changed Original file line number Diff line number Diff 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 [ ] > {
You can’t perform that action at this time.
0 commit comments