[英]Array map returning array of undefined when should return array of objects
为什么
['a', 'b', 'c'].map((x) => { letter: x })
返回一个undefined数组
和
['a', 'b', 'c'].map((x) => [{ letter: x }][0])
正确返回一个对象数组?
你需要在()
包装对象
var result = ['a', 'b', 'c'].map((x) => ({ letter: x })) console.log(result)
缺少显式return语句的函数将返回undefined。 () => {}
等同于function(){}
。 x => x
相当于function(x){ return x;}
因此,没有{}
箭头函数将返回表达式的计算值。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.