Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Replace characters in string array Javascript

I have defined and populated an array called vertices. I am able to print the output to the JavaScript console as 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 wish to remove the 'v' character from each element. I have tried using the .replace() function as below:

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

Upon printing the array to the console after this code I see the same output as before, with the 'v's still present.

Could anyone tell me how to solve this?

like image 784
petehallw Avatar asked Nov 04 '14 18:11

petehallw


Video Answer


1 Answers

Strings are immutable, so you just have to re-assign their value:

vertices[x] = vertices[x].replace('v ', '');
like image 58
p e p Avatar answered Oct 14 '22 21:10

p e p