【问题标题】:how to use C# dictionary in typescript?如何在打字稿中使用 C# 字典?
【发布时间】:2018-12-18 09:26:09
【问题描述】:

我的 c# 中有字典对象。但我需要迁移到打字稿。我是打字稿的新手。我真的不知道,如何在打字稿中使用字典。

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}
            };

【问题讨论】:

标签: c# typescript dictionary


【解决方案1】:

您可以在记录

的帮助下创建类型
type TestType = Record<string, number>;
const stepInfo: TestType = [
 "a": 1,
 "b": 2
];

【讨论】:

    【解决方案2】:

    您可以使用 Map 对象。 Map 是 ES6 中引入的一种新数据结构,它允许您将键映射到值,而没有使用对象的缺点。

    例如

    let map = new Map();
    map.set("A",1);
    map.set("B",2);
    map.set("C",3);
    

    【讨论】:

      【解决方案3】:

      在你的 c# 端,使用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);
           }
        }
      }
      

      在你的 JS 端,你的结果会是这样的:

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

      然后像这样使用它:Result.ADResult['AD'], 但这在大多数用法中没有意义,您将需要这个: 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]
          };
      });
      

      Object.keys(result).forEach(....)
      

      【讨论】:

        【解决方案4】:

        你可以这样做:

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

        然后初始化项目:

        lengthsByCountry["AD"] = 24;
        

        据我所知,目前还没有内联初始化的直接映射。

        【讨论】:

          【解决方案5】:

          它只是一个 javascript 对象。

           export interface Dto {
                lengthsByCountry: { [name: string]: string };
              }
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2020-07-03
            • 1970-01-01
            • 1970-01-01
            • 2019-11-12
            • 2020-06-20
            • 1970-01-01
            • 2017-12-07
            • 2017-12-29
            相关资源
            最近更新 更多