Replace characters in string array Javascript

匿名 (未验证) 提交于 2019-12-03 08:44:33

问题:

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?

回答1:

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

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


回答2:

Should be

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

Because replace returns value, and doesn't change initial string.



回答3:

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


易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!