简体   繁体   English

替换字符串数组Javascript中的字符

[英]Replace characters in string array Javascript

I have defined and populated an array called vertices . 我已经定义并填充了一个名为vertices的数组。 I am able to print the output to the JavaScript console as below: 我能够将输出打印到JavaScript控制台,如下所示:

["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. 但是我希望从每个元素中删除'v'字符。 I have tried using the .replace() function as below: 我尝试过使用.replace()函数,如下所示:

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. 在此代码之后将阵列打印到控制台后,我看到与之前相同的输出,并且'v仍然存在。

Could anyone tell me how to solve this? 谁能告诉我如何解决这个问题?

字符串是不可变的,因此您只需重新分配它们的值:

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

Should be 应该

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

Because replace returns value, and doesn't change initial string. 因为replace 返回值,并且不更改初始字符串。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM