数组映射返回未定义的数组时应返回对象数组 [英] Array map returning array of undefined when should return array of objects
问题描述
为什么
['a','b','c']。map((x)=> {letter :x})
返回一个未定义的数组
['a', 'b', 'c'].map((x) => { letter: x })
returns a array of undefined
和
['a','b','c']。map((x)=> [{letter:x}] [0])
正确返回一个对象数组?
['a', 'b', 'c'].map((x) => [{ letter: x }][0])
returns a array of objects correctly?
推荐答案
因为
-
您使用大括号作为阻止声明 。
你有字母
作为 label 。
x
只是一个没有动作的值。
x
is just a value without some action.
返回 undefined
是函数的标准返回值机智hout任何带有值的return语句。
The return of undefined
is the standard return value of a function without any return statement with value.
要返回默认值以外的值,函数必须具有
return
声明指定要返回的值。没有return语句的函数将返回默认值。如果是构造函数,则调用此方法使用new
关键字,默认值是其此参数的值。对于所有其他函数,默认返回值是未定义的。
To return a value other than the default, a function must have a
return
statement that specifies the value to return. A function without a return statement will return a default value. In the case of a constructor called with thenew
keyword, the default value is the value of its this parameter. For all other functions, the default return value is undefined.
正确的调用映射对象。
console.log(['a', 'b', 'c'].map(x => ({ letter: x })));
这篇关于数组映射返回未定义的数组时应返回对象数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!