modifing the array values

Solutions on MaxInterview for modifing the array values by the best coders in the world

showing results for - "modifing the array values"
Lamia
24 Nov 2020
1//Modifying the Array values
2let colors = ["blue", "red", "black"];
3
4//Higher Order Array function map() doesn't work
5//because it will be created a new element "x" 
6//and the value of that element will be modified
7colors.map(x => x.replace(x.toUpperCase()));
8console.log(colors); //Output: ["blue", "red", "black"]
9
10//"for...of" loop doesn't work too, same reason.
11for (color of colors){
12  color = color.toUpperCase();
13}
14console.log(colors); //Output: ["blue", "red", "black"]
15
16//to modify the values of the origin Array, 
17//it will be needed an access to the direct 
18//placeholder of that value (colors[0...2]).
19for (let i = 0; i < colors.length; i++){
20  colors[i] = colors[i].toUpperCase();
21}
22console.log(colors); //Output: ["BLUE", "RED", "BLACK"]