【问题标题】:How to convert enum into a key,value array in typescript?如何将枚举转换为打字稿中的键值数组?
【发布时间】:2023-04-01 14:05:02
【问题描述】:
var enums = {
  '1': 'HELLO',
  '2' : 'BYE',
  '3' : 'TATA'
  };

我希望能够将它转换成这样的数组,

[
  {
    number:'1',
    word:'HELLO'
  },
  {
    number:'2',
    word:'BYE'
  },
  {
    number:'3',
    word:'TATA'
  }
]

我看到的所有解决方案都是由键或值组成的数组。

【问题讨论】:

  • 什么是{'1', 'HELLO'}?它既不是有效的 JavaScript 也不是有效的 TypeScript。
  • 你为什么期待{number:'2', word: 'TATA'}
  • @jcalz 显然是一个错字

标签: javascript arrays typescript enums casting


【解决方案1】:

您可以使用short hand properties 映射条目。

var enums = { 1: 'HELLO', 2: 'BYE', 3: 'TATA' },
    objects = Object.entries(enums).map(([number, word]) => ({ number, word }));

console.log(objects);
.as-console-wrapper { max-height: 100% !important; top: 0; }

【讨论】:

    【解决方案2】:

    另一种选择是使用for ... in 循环来迭代enums 键并构造所需的对象数组。

    var enums = {
      '1': 'HELLO',
      '2' : 'BYE',
      '3' : 'TATA'
    };
    
    let res = [];
    
    for (key in enums)
    {
        res.push({number: key, word: enums[key]});    
    }
    
    console.log(res);
    .as-console {background-color:black !important; color:lime;}
    .as-console-wrapper {max-height:100% !important; top:0;}

    【讨论】:

      【解决方案3】:

      您可以将Object.entriesmap 使用为所需的格式

      var enums = {
        '1': 'HELLO',
        '2' : 'BYE',
        '3' : 'TATA'
        };
        
      let op = Object.entries(enums).map(([key, value]) => ({ number:key, word:value }))
      
      console.log(op)

      【讨论】:

      • 你怎么知道这个预期的输出?
      • @brk 这是在问题伙伴中指定的。 I want to be able to convert that into an array that looks like this,
      • 感谢您的回答。这给了我输出 [ { "1": "HELLO" }, { "2": "BYE" }, { "3": "TATA" } ] 我需要它是 [ { "1","HELLO" }, { "2","BYE" }, { "3","TATA" } ] ,而不是 :
      • @user7434041 你能描述一下你期望的数据类型吗? { "1","HELLO" } 不是有效的 JavaScript 或打字稿。它几乎看起来像一个 python 元组
      • @user7434041 在您需要键/值对的对象中,格式为 --> key:value
      【解决方案4】:

      您可以将Object.entries() 与 foreach 一起使用并将其推送到这样的数组中

      var enums = {
          '1': 'HELLO',
          '2' : 'BYE',
          '3' : 'TATA'
          };
      
      var enumArray = []
      Object.entries(enums).forEach(([key, value]) => enumArray.push({number : key, word : value}));
      
      console.log(enumArray);

      【讨论】:

        【解决方案5】:

        你可以使用 Object.keys 和 map

        var obj = {
          '1': 'HELLO',
          '2' : 'BYE',
          '3' : 'TATA'
        };
        
        
        const result = Object.keys(obj).map(el => {
          return {
            number: el,
            word: obj[el]
          }
        })
        
        console.log(result)

        【讨论】:

          【解决方案6】:

          需要创建一个 Map 类型的对象,然后使用 get 获取值 方法如 outData.get("1")

          var obj = {
              '1': 'HELLO',
              '2': 'BYE',
              '3': 'TATA'
          };
          var outData = new Map();
          Object.keys(obj).forEach(function (e) {
              outData.set(e, obj[e])
          });
          

          要获取数据,请使用 outData.get("key")

          现在输出的数据会喜欢-

          Map(3) {"1" => "HELLO", "2" => "BYE", "3" => "TATA"}
          

          【讨论】:

            猜你喜欢
            • 2019-03-04
            • 2020-09-06
            • 2018-11-09
            • 2019-10-30
            • 2019-09-25
            • 2018-07-05
            • 2021-02-14
            • 2023-03-25
            相关资源
            最近更新 更多