Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to use C# dictionary in typescript?

I have dictionary objects in my c#. but I need to migrate to typescript. I am new to typescript. I really don't know, how to use dictionary in typescript.

C#:

 LengthsByCountry = new Dictionary<string, int>
            {
                {"AD", 24}, {"AL", 28}, {"AT", 20}, {"BA", 20}, 
                {"BE", 16}, {"BG", 22}, {"CH", 21}, {"CY", 28}, 
                {"CZ", 24}, {"DE", 22}, {"DK", 18}, {"EE", 20}, 
                {"ES", 24}, {"FI", 18}, {"FO", 18}, {"FR", 27}, 
                {"GB", 22}, {"GE", 22}, {"GI", 23}, {"GL", 18}, 
                {"GR", 27}, {"HR", 21}, {"HU", 28}, {"IE", 22}, 
                {"IL", 23}, {"IS", 26}, {"IT", 27}, {"LB", 28}, 
                {"LI", 21}, {"LT", 20}, {"LU", 20}, {"LV", 21}, 
                {"MC", 27}, {"ME", 22}, {"MK", 19}, {"MT", 31}, 
                {"MU", 30}, {"NL", 18}, {"NO", 15}, {"PL", 28}, 
                {"PT", 25}, {"RO", 24}, {"RS", 22}, {"SA", 24}, 
                {"SE", 24}, {"SI", 19}, {"SK", 24}, {"SM", 27}, 
                {"TN", 24}, {"TR", 26}
            };
like image 435
Arun Kumar Avatar asked Jul 10 '18 13:07

Arun Kumar


People also ask

How is C used?

C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...

Is C easy for beginners?

C language is simple and easy to learn. C is a machine independent language, which means a C program written one machine can run on another machine without requiring a code change. C is a compiler based language and it supports only useful features which makes the compilation of C file fast.


1 Answers

You can do something like this:

let lengthsByCountry: { [key: string]: number; } = {};

Then initialize the items:

lengthsByCountry["AD"] = 24;

There's no direct mapping for the inline initialization at present, as far as I know.

like image 81
Lloyd Avatar answered Sep 17 '22 21:09

Lloyd