为什么在Javascript中使用.push()无法直接返回值?

编程入门 行业动态 更新时间:2024-10-25 16:24:17
本文介绍了为什么在Javascript中使用.push()无法直接返回值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

以下面的flatten函数为例,为什么我不能直接在累加器上使用 return 。 Push()

using below flatten function as example, why cant I use return directly on the accumulator. Push()

function flatten(array){ return reduce(array,function(accumulator, value){ if(Array.isArray(value)){ each(value,function(value1){ return accumulator.push(value1); //as shown }) }else{ return accumulator.push(value); //as shown } },[]); };

相反,我需要在单独的行上返回累加器吗?

Instead, I need to return accumulator on a separate line?

function flatten(array){ return reduce(array,function(accumulator, value){ if(Array.isArray(value)){ each(value,function(value1){ accumulator.push(value1); }) }else{ accumulator.push(value); } return accumulator //as shown },[]); };

但是如果使用诸如.concat()之类的方法,我将能够使用返回 ...

but if use method such as .concat() I will be able to use return on it...

推荐答案

推送方法返回Array的新长度。

The push method returns the new length of the Array.

用于推送的MDN文档

concat 方法返回一个全新的数组,其中包含两个数组的组合。

The concat method returns a brand new array containing a combination of the two arrays.

用于concat的MDN文档

更多推荐

为什么在Javascript中使用.push()无法直接返回值?

本文发布于:2023-11-26 03:31:54,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1632465.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:返回值   Javascript   push

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!