21

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}
            };
Arun Kumar
  • 1,449
  • 1
  • 17
  • 33
  • 3
    not done it personally but is thia any help? https://stackoverflow.com/questions/13631557/typescript-objects-as-dictionary-types-as-in-c-sharp – Fuzzybear Jul 10 '18 at 13:41

5 Answers5

36

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.

Lloyd
  • 29,197
  • 4
  • 84
  • 98
21

You can use Map object. Map is a new data structure introduced in ES6 which lets you map keys to values without the drawbacks of using Objects.

For example

let map = new Map();
map.set("A",1);
map.set("B",2);
map.set("C",3);
Israel.z
  • 321
  • 2
  • 6
5

It's just a javascript object.

 export interface Dto {
      lengthsByCountry: { [name: string]: string };
    }
MistyK
  • 6,055
  • 2
  • 42
  • 76
4

you can create a type with the help of Record

type TestType = Record<string, number>;
const stepInfo: TestType = [
 "a": 1,
 "b": 2
];
Sadid Khan
  • 1,836
  • 20
  • 35
3

in your c# side, use return Json(data);

namespace YourNamespace.Controllers
{
  [Route("[controller]/[action]")]
  public class YourController : Controller
  {
     private readonly IYourInterface _client;
     public YourController(IYourInterface client)
     {
         this._client = client;
     }
     [HttpPost]
     public async Task<JsonResult> GetYourDictionary(parms[] yourParms)
     {
         //Here is your dictionary
         var data = await _client.GetYourDictionary(yourParms);
         return Json(data);
     }
  }
}

in your JS side, your result will be like this:

{
  AD:24,
  AL:28,
  ....
}

then use it like this: Result.AD or Result['AD'], but it makes no sense in most of usages, you will need this: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/keys

Object.keys(result).map(function(key){
    return {
    key: key,
    value: result[key]
    };
});

or

Object.keys(result).forEach(....)
Dongdong
  • 2,208
  • 19
  • 28