是否有可能在JavaScript做的foreach何时改变数组的值? [英] is it possible to change values of the array when doing foreach in javascript?
本文介绍了是否有可能在JavaScript做的foreach何时改变数组的值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
例如:
var arr = ["one","two","three"];
arr.forEach(function(part){
part = "four";
return "four";
})
alert(arr);
该阵列仍然与它的原始值,有没有什么办法有从迭代函数写访问数组的元素呢?
The array is still with it's original values, is there any way to have writing access to array's elements from iterating function ?
推荐答案
回调函数传递的元素,索引和数组本身。
The callback is passed the element, the index, and the array itself.
arr.forEach(function(part, index, theArray) {
theArray[index] = "hello world";
});
这篇关于是否有可能在JavaScript做的foreach何时改变数组的值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文