【问题标题】:'Unexpected token' syntax error in object returned by arrow function [duplicate]箭头函数返回的对象中的“意外标记”语法错误[重复]
【发布时间】:2016-04-14 11:06:41
【问题描述】:

这里是有问题的代码:

const data =
  results.responses.map((response, idx) =>
    { id: idx+1,
      name: response.name,
      email: response.email,
      comment: response.comment
    }
  )

我正在使用 babel 将 es6 代码转换为 javascript。这是错误信息:

Module build failed: SyntaxError: /Users/antkong/dev/project/form.js: Unexpected token (60:14)
  58 |       results.responses.map((response, idx) =>
  59 |         { id: idx+1,
> 60 |           name: response.name,
     |               ^
  61 |           email: response.email,
  62 |           comment: response.comment
  63 |         }

为什么会出现语法错误?

【问题讨论】:

    标签: javascript ecmascript-6


    【解决方案1】:

    在您的示例中,JavaScript 将 {} 视为 block statement 而不是对象文字。将您的对象括在括号中(()),它将起作用。

    更正的代码:

    const data =
      results.responses.map((response, idx) =>
        ({ id: idx+1,
          name: response.name,
          email: response.email,
          comment: response.comment
        })
      )
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-06-25
      • 1970-01-01
      • 2018-06-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多