EN
                                
                            
                        JavaScript - change values in array during foreach
                                    0
                                    points
                                
                                In this article, we would like to show you how to change values in array during foreach in JavaScript.
Quick solution (ES6):
// ONLINE-RUNNER:browser;
const letters = ['a', 'b', 'c'];
letters.forEach((element, index, array) => array[index] = 'x');
console.log(letters); // [ 'x', 'x', 'x' ]
Alternative solution (ES5)
In this example, we alternatively use forEach() with a second argument (context), which will be used as the value of this in each call to the callback function.
// ONLINE-RUNNER:browser;
var letters = ['a', 'b', 'c'];
letters.forEach(function(element, index) {
    this[index] = 'x';
}, letters);
console.log(letters); // [ 'x', 'x', 'x' ]
Warning:
This solution won't work with arrow functions.
