rsk82 rsk82 - 1 year ago 45
Javascript Question

is it possible to change values of the array when doing foreach in javascript?


var arr = ["one","two","three"];

part = "four";
return "four";


The array is still with it's original values, is there any way to have writing access to array's elements from iterating function ?

Answer Source

The callback is passed the element, the index, and the array itself.

arr.forEach(function(part, index, theArray) {
  theArray[index] = "hello world";