小编R S*_*ant的帖子

使用地图的胖箭头用法

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)

javascript functional-programming

0
推荐指数
1
解决办法
353
查看次数

标签 统计

functional-programming ×1

javascript ×1