Replace characters in a Javascript string array

I defined and populated an array called vertices . I can print the output in the JavaScript console as shown below:

 ["v 2.11733 0.0204144 1.0852", "v 2.12303 0.0131256 1.08902", "v 2.12307 0.0131326 1.10733" ...etc. ] 

However, I want to remove the "v" character from each item. I tried this using the .replace() function as shown below:

 var x; for(x = 0; x < 10; x++) { vertices[x].replace('v ', ''); } 

After printing the array to the console after this code, I see the same result as before, with 'v still present. Can someone tell me how to solve this?

+5
source share
3 answers

Strings are immutable, so you just need to reassign their value:

 vertices[x] = vertices[x].replace('v ', ''); 
+9
source

Must be

 vertices[x]=vertices[x].replace('v ', ''); 

Because the replacement returns the value and does not change the starting line.

+4
source
 vertices[x] = vertices[x].replace('v ', ''); 
0
source

Source: https://habr.com/ru/post/1206185/


All Articles