Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to initialize map with elements in typescript?

Tags:

typescript

I woule like to create this map and add few elements to it while initialization. The key should be the name of the person. How this can be achieved?

 let person1: Person = ...
 let person2: Person = ...
 let map: { [key: string]: Person } = {
        person1.name = person1,
        person2.name = person2
      };

but this doesn't compile. How to put some elements inside?

like image 383
Yoda Avatar asked Sep 21 '17 09:09

Yoda


1 Answers

You can go with:

let map: { [key: string]: Person } = {
    [person1.name]: person1,
    [person2.name]: person2
};
like image 147
Aleksey L. Avatar answered Nov 15 '22 12:11

Aleksey L.