Javascript 期望返回箭头中的值;函数数组回调返回。为什么?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/48163744/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 04:02:19  来源:igfitidea点击:

Expected to return a value in arrow; function array-callback-return. Why?

javascriptarraysecmascript-6arrow-functions

提问by Chef1075

I'm having some issues understanding why I'm getting a compile warning on this piece of my react code

我在理解为什么在这段 React 代码上收到编译警告时遇到了一些问题

fetch('/users')
        .then(res => res.json())
        .then(data => {
            data.map(users => {
                console.log(users);
            });
        });

The warning I'm getting is Expected to return a value in arrow function array-callback-return

我得到的警告是 Expected to return a value in arrow function array-callback-return

However I'm still get the json object values from my /users, and they are printed to the console individually. The object is:

但是,我仍然从我的 .json 获取 json 对象值/users,并将它们单独打印到控制台。对象是:

    {
        id: 1,
        username: "Foo"
    }, {
        id: 2,
        username: "Bar"
    }

Am I missing a return statement, or am I missing something with how mapreturns values after a .then()? I'm unclear on why the compile warning would appear at all.

我是否缺少 return 语句,或者我是否缺少有关如何map在 a 之后返回值的内容.then()?我不清楚为什么会出现编译警告。

回答by croraf

data.mapfunction (check Array.mapspecification: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/map) converts one array (datain your case) to a new array. This mapping is defined by the argument of data.map. The argument of data.map(the callback function), in your case the arrow function users => {console.log(users);}, must return a value. By returning a value for each array element is how data.mapdefines the mapping.

data.map函数(检查Array.map规范:https: //developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/map)将一个数组(data在您的情况下)转换为一个新数组。此映射由 的参数定义data.mapdata.map(回调函数)的参数,在您的情况下是箭头函数users => {console.log(users);},必须返回一个值。通过为每个数组元素返回一个值是如何data.map定义映射的。

But in your case the callback function does not return anything, just console.logs. In your case you can use data.forEach(https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/forEach) as you don't use Array.mapfunctionality.

但是在您的情况下,回调函数不返回任何内容,仅返回console.logs。在您的情况下,您可以使用data.forEachhttps://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/forEach),因为您不使用Array.map功能。

NOTE: If you decide to use data.mapyou should have a singular (rather than plural) name as the argument of callback: data.map(user => {console.log(user);});which is now called for each user.

注意:如果您决定使用,data.map您应该有一个单数(而不是复数)名称作为 callback:data.map(user => {console.log(user);});现在为每个用户调用的参数。

回答by Rodius

If you don't need to mutate the array and just do the console.log() you can do data.forEach() instead. It shouldn't give you the warning. Map expects you to return a value after you've transformed the array.

如果您不需要改变数组而只需执行 console.log() ,则可以执行 data.forEach() 。它不应该给你警告。Map 期望您在转换数组后返回一个值。

fetch('/users')
        .then(res => res.json())
        .then(data => {
            data.forEach(users => {
                console.log(users);
            });
        });

回答by Cecil

From MDN:

来自 MDN:

The map() method creates a new array with the results of calling a provided function on every element in the calling array.

map() 方法使用对调用数组中的每个元素调用提供的函数的结果创建一个新数组。

That means the map method has to be returned. So,you should change your code like this:

这意味着必须返回 map 方法。所以,你应该像这样改变你的代码:

fetch('/users')
    .then(res => res.json())
    .then(data => {
        data.map(users => {
            return console.log(users);
        });
    });

or use forEach() instead of map()

或使用 forEach() 而不是 map()