简体   繁体   English

.forEach() Javascript 函数返回一个数组?

[英].forEach() Javascript function is returning an Array?

Generally, It is said that 'forEach' doesn't return an array, whereas 'map' returns one.一般来说,据说'forEach'不返回数组,而'map'返回一个。 However, In my case, it is returning one.但是,就我而言,它正在返回一个。 Below posted is my code.下面贴出的是我的代码。 I am kind of confused.我有点困惑。 Any help would be appreciated.任何帮助,将不胜感激。

//Code Snippet //
var arr = [1, 3, 2];
var arr_temp = [];

arr.forEach(function (i) {
  return arr_temp.push(i + i);
});

console.log(arr_temp);

I am getting the output as: [2,6,4]我得到的输出为:[2,6,4]

It doesn't return anything.它不返回任何东西。 You can check that by logging the result of the forEach call:您可以通过记录forEach调用的结果来检查:

 var arr = [1, 3, 2]; var arr_temp = []; console.log(arr.forEach(function (i) { return arr_temp.push(i + i); })); // undefined console.log(arr_temp); // 2, 6, 4

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM