Javascript 在arrow函数中使用ComputedPropertyName创建对象文字

Javascript 在arrow函数中使用ComputedPropertyName创建对象文字,javascript,ecmascript-6,arrow-functions,computed-properties,Javascript,Ecmascript 6,Arrow Functions,Computed Properties,如何将其转换为: const数组=[10,0,90,80,50,0,60]; 设id=1; const studentScores=array.map(函数(分数、索引){ 返回{[索引]:分数,学生id:id} }); console.log(studentScores)您必须将对象文字括起来,以使解析器确信它是一个对象文字: const studentScores = array.map((score, index) => ({ [index]: score, student_id:

如何将其转换为:

const数组=[10,0,90,80,50,0,60];
设id=1;
const studentScores=array.map(函数(分数、索引){
返回{[索引]:分数,学生id:id}
});

console.log(studentScores)
您必须将对象文字括起来,以使解析器确信它是一个对象文字:

const studentScores = array.map((score, index) => ({ [index]: score, student_id: id }) );

通过将其包装在括号中,解析器被迫将其解释为表达式而不是语句块。
{
字符具有这种模糊性,当它是语句中的第一件事时,解析器总是假定“语句块”就是它所看到的内容。

您必须将对象文字括起来,以使解析器确信它是一个对象文字:

const studentScores = array.map((score, index) => ({ [index]: score, student_id: id }) );
通过将其包装在括号中,解析器被迫将其解释为表达式而不是语句块。
{
字符具有这种歧义性,当它是语句中的第一件事时,解析器总是假定“语句块”就是它所看到的。

要使用箭头函数隐式返回对象,请使用括号将其包裹起来:

const studentScores = array.map((score, index) => ({ [index]: score, student_id: id }) );
要使用arrow函数隐式返回对象,请使用括号将其包裹起来:

const studentScores = array.map((score, index) => ({ [index]: score, student_id: id }) );

隐式?你不是指显式吗?@Cerbrus是的,隐式。当你输入一个返回时,它是显式的。在这里,我不写
return
keywork,所以它是隐式的,我从来没有听说过它被描述为“隐式的”return…有趣。隐式?你不是指显式吗?@Cerbrus是的,隐式。当你放一个return时,它是显式的。在这里,我不写
return
keywork,所以它是隐式的,我从来没有听说过被描述为“隐式”的return…有趣。