【问题标题】:Converting javascript array of objects to array of other objects using map method [duplicate]使用map方法将javascript对象数组转换为其他对象数组[重复]
【发布时间】:2016-07-17 19:02:07
【问题描述】:

我想将一个数组转换为另一种数组。 实际上我正在使用打字稿。 我在这里做错了什么:

//terminals is an array of objects. 
    groupOptions = terminals.map(trm => {
                id: trm.TerminalID, 
                text: trm.TerminalName, 
                selected: true
            });

智能抱怨大括号的主体。对于它们,我的意思是一个对象,可能智能认为它的匿名方法体。我该如何解决这个问题?

【问题讨论】:

标签: javascript arrays object dictionary typescript


【解决方案1】:

尝试用括号将大括号括起来,如下所示:

    //terminals is an array of objects. 
    groupOptions = terminals.map(trm => ({
                id: trm.TerminalID, 
                text: trm.TerminalName, 
                selected: true
            }) );

问题在于 JavaScript 运行时将大括号作为函数的开始/结束。

【讨论】:

  • 它不仅仅是 IDE。 JavaScript 运行时也是如此,因此即使在 ES6 raw(无 TypeScript)中也是 语法错误
  • 感谢您的评论,我会更新我的答案
  • 最优雅,谢谢
猜你喜欢
  • 2021-01-04
  • 2019-10-22
  • 2021-01-09
  • 1970-01-01
  • 2020-02-06
  • 2018-03-18
  • 2015-04-21
  • 2023-03-19
  • 2017-01-14
相关资源
最近更新 更多