数组映射返回未定义的数组时应返回对象数组 [英] Array map returning array of undefined when should return array of objects

查看:118
本文介绍了数组映射返回未定义的数组时应返回对象数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么

['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 the new 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆