JavaScript新手在这里.在尝试使用带有胖箭头的Arrray.map时,我遇到了编译错误.下面是我的示例代码以及错误.
var employeesWithComplexLocation = [{
"name": "jon",
"location": {
"country": "usa",
"city": "austin"
}
}, {
"name": "jane",
"location": {
"country": "usa",
"city": "houston"
}
}, {
"name": "mary",
"location": {
"country": "usa",
"city": "dallas"
}
}];
var employeesWithOnlyCity = employeesWithComplexLocation.map(function(element) {
return {
name: element.name,
location: element.location.city
};
});
console.log(employeesWithOnlyCity);
console.log('Now using fat arrow:')
employeesWithOnlyCity = employeesWithComplexLocation.map(e => {
name: e.name,
location: e.location.city
});
console.log(employeesWithOnlyCity);Run Code Online (Sandbox Code Playgroud)
Array.map按预期使用匿名函数工作,但是当我使用胖箭头时,会给出以下错误.
employeesWithOnlyCity = employeesWithComplexLocation.map(e => {
name: e.name,
location: e.location.city
});
SyntaxError: Unexpected token …Run Code Online (Sandbox Code Playgroud)