javascript JS foreach 循环更新数组元素

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/48029759/
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-10-29 07:45:14  来源:igfitidea点击:

JS foreach loop to update an array element

javascript

提问by York Gong

I am currently learning JavaScript. And I tried to use a foreach loop to update my elements in an array. But the problem is that the "console.log" result always been the same array as before. Below is the code. Can anyone help tell the problem?

我目前正在学习 JavaScript。我尝试使用 foreach 循环来更新数组中的元素。但问题是“console.log”的结果总是和以前一样的数组。下面是代码。谁能帮忙说说问题?

var test = [12, 929, 11, 3, 199, 1000, 7, 1, 24, 37, 4,
    19, 300, 3775, 299, 36, 209, 148, 169, 299,
    6, 109, 20, 58, 139, 59, 3, 1, 139
];

var addNum = function(element,index,array){
    if(element%3===0)
    {
        element += 100;
    }
};

test.forEach(addNum);
console.log(test);

回答by Bsalex

That's because in JavaScript arguments are passed by value, not by reference.
So changing elementargument does nothing.

那是因为在 JavaScript 中参数是按值传递的,而不是按引用传递。
所以改变element论点没有任何作用。

In your case, it is better to use map, like this:

在您的情况下,最好使用map,如下所示:

var addNum = function(element,index,array){
    if(element%3===0)
    {
        return element + 100;
    }

    return element
};

const result = test.map(addNum);
console.log(result);

If you really need to use forEach- you could do it like this:

如果你真的需要使用forEach- 你可以这样做:

var test = [12, 929, 11, 3, 199, 1000, 7, 1, 24, 37, 4,
    19, 300, 3775, 299, 36, 209, 148, 169, 299,
    6, 109, 20, 58, 139, 59, 3, 1, 139
];

var addNum = function(element,index,array){
    if(element%3===0)
    {
        array[index] += 100;
    }
};

test.forEach(addNum);
console.log(test);

But this is, in my opinion, a bad practice.
forEachis designed for doing something with each element in an array without changing it, but mapis specifically designed to create new array running a function on each element of the provided array.
Also see discussion here Is there a difference between foreach and map?

但在我看来,这是一种不好的做法。
forEach旨在对数组中的每个元素执行某些操作而不更改它,但map专门用于创建新数组,在提供的数组的每个元素上运行一个函数。
另请参阅此处的讨论foreach 和 map 之间有区别吗?

回答by Sébastien

In your addNumfunction, elementis just an argument. When you modify it , you are only modifying the value inside the function, not the actual element in the array.

在你的addNum函数中,element只是一个参数。当你修改它时,你只是修改了函数内部的值,而不是数组中的实际元素。

To modify the array, you need to target the element:

要修改数组,您需要定位元素:

var test = [12, 929, 11, 3, 199, 1000, 7, 1, 24, 37, 4,
    19, 300, 3775, 299, 36, 209, 148, 169, 299,
    6, 109, 20, 58, 139, 59, 3, 1, 139
];

var addNum = function(element, index, array) {
    if (element % 3 === 0) {
        array[index] = element + 100;
    }
};
test.forEach(addNum);
console.log(test);

Note that in JavaScript you can directly pass an anonymous function to forEach():

请注意,在 JavaScript 中,您可以直接将匿名函数传递给forEach()

test.forEach(function(element, index, array) {
    if (element % 3 === 0) {
        array[index] = element + 100;
    }
});