为什么
['a', 'b', 'c'].map((x) => { letter: x })
返回一个未定义的数组
and
['a', 'b', 'c'].map((x) => [{ letter: x }][0])
正确返回对象数组?
Because
-
您可以使用大括号作为块语句 https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/Arrow_functions.
-
你有letter
as a label https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/label.
-
x
只是一个没有任何操作的值。
-
返回的undefined
是 a 的标准返回值function https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions没有任何带有值的返回语句。
要返回默认值以外的值,函数必须具有return https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/return指定要返回的值的语句。没有 return 语句的函数将返回默认值。如果是构造函数 https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/constructor调用与new https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/new关键字,默认值是其this参数的值。对于所有其他函数,默认返回值未定义。
正确调用映射对象。
console.log(['a', 'b', 'c'].map(x => ({ letter: x })));
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)