6

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?

1 Answer 1

14

You can go with:

let map: { [key: string]: Person } = {
    [person1.name]: person1,
    [person2.name]: person2
};
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.