简体   繁体   English

forEach不会改变我传入的数组

[英]forEach does not mutate an array that I pass in

I've created a simple forEach function and I'm trying to understand why, when I run it with myArray , it doesn't mutate the array even though I run element*2 . 我已经创建了一个简单的forEach函数,我试图理解为什么,当我使用myArray运行它时,即使我运行element*2 ,它也不会改变数组。

function forEach(array, callback) {
  for (var i = 0; i < array.length; i++) {
    callback(array[i],i,array)
  };
}

var myArray = [1,2,3]
forEach(myArray,function(element){element*2})
console.log(myArray)///[1,2,3]

You have to modify the array in the for loop, like this: 您必须在for循环中修改数组,如下所示:

function forEach(array, callback) {
  for (var i = 0; i < array.length; i++) {
    array[i] = callback(array[i],i,array)
  };
}

var myArray = [1,2,3]
forEach(myArray,function(element){return element*2})
console.log(myArray)

As we were discussing in the comments, the best way would be to implement something like the map function: https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Array/map 正如我们在评论中讨论的那样,最好的方法是实现类似map函数: https//developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Array/map

function map(array, callback) {
  var out = [];
  for (var i = 0; i < array.length; i++) {
    out.push(callback(array[i],i,array))
  };

  return out;
}

var myArray = [1,2,3]
var myOtherArray = map(myArray,function(element){return element*2})
console.log(myArray)
console.log(myOtherArray)

This way myArray is not touched, you create a new one. 这样myArray没有触及,你创建一个新的。 This is usually the best option, but sometimes you may want to modify it in place, because it is huge or for some other (good?) reason. 这通常是最好的选择,但有时你可能想要在适当的位置修改它,因为它很大或者其他(好的?)原因。 In that case you can use the first option. 在这种情况下,您可以使用第一个选项。

You should assign new array element value, because primitive types (like numbers in your case) are immutable, so element * 2 does not modify element . 您应该分配新的数组元素值,因为基本类型(例如您的数字)是不可变的,因此element * 2不会修改element

To do the job, you should not touch you current forEach implementation, because forEach is not supposed to return values from callback (this is not map). 要完成这项工作,你不应该触及当前的forEach实现,因为forEach不应该从回调中返回值(这不是map)。 In this case you should do something like this: 在这种情况下,你应该做这样的事情:

 function forEach(array, callback) { for (var i = 0; i < array.length; i++) { callback(array[i], i, array); } } var myArray = [1,2,3]; forEach(myArray, function(element, i, arr) { arr[i] = element * 2; }); document.write(JSON.stringify( myArray )); 

Yep, bad answer. 是的,回答不好。 This [snippet] would do it though. 这个[片段]会这样做。

Anyway, in modern browsers we have Array.forEach to availability 无论如何,在现代浏览器中,我们有Array.forEach 可用性

 function foreach(array, callback) { for (var i = 0; i < array.length; i++) { array[i] = callback(array[i]); // ^ assign the new value (from the callback function) }; } var myArray = [1,2,3] foreach( myArray, function (element){ return element * 2 } ); // ^ return the new value document.querySelector('#result').textContent = myArray; 
 <pre id="result"></pre> 

This should work, explicitly assigning the variable. 这应该工作,明确分配变量。

function forEach(array, callback) {
  for (var i = 0; i < array.length; i++) {
    array[i] = callback(array[i])
  };
}

var myArray = [1,2,3]
forEach(myArray,function(element){return element*2})
console.log(myArray)///[1,2,3]

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

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