Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/shell/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript ReactJS-数组回调返回_Javascript_Reactjs - Fatal编程技术网

Javascript ReactJS-数组回调返回

Javascript ReactJS-数组回调返回,javascript,reactjs,Javascript,Reactjs,在本例中,我对对象和它的工作方式进行求和,正如我所希望的,问题是以下消息: 应在arrow函数数组回调返回中返回值 代码如下: data.map((current,index, array) => { const prevVisitors = array[index - 1 ] ? array[index - 1].visitors : 0 current.visitors += prevVisitors }) 如果没有要返回的内容(因为它只是对道具的修改),我应该使用什

在本例中,我对对象和它的工作方式进行求和,正如我所希望的,问题是以下消息:

应在arrow函数数组回调返回中返回值

代码如下:

data.map((current,index, array) => {
    const prevVisitors = array[index - 1 ] ? array[index - 1].visitors : 0
    current.visitors += prevVisitors
})
如果没有要返回的内容(因为它只是对道具的修改),我应该使用什么作为返回语句?在什么情况下这个错误是有用的?

您的代码应该是

data.map((current,index, array) => {
    const prevVisitors = array[index - 1 ] ? array[index - 1].visitors : 0
    return current.visitors += prevVisitors
})

map
的工作原理是,您需要返回更改后的值,以便在更改后
返回当前值
,即使在这种情况下,
forEach
可能更合适我没想到会这样,这确实是对map函数的滥用,谢谢!是的,我一开始使用了这个,但是对于@quirimo的答案,我觉得更好,使用forEach比返回当前值更好,谢谢