替换字符串数组Javascript中的字符 [英] Replace characters in string array Javascript

查看:55
本文介绍了替换字符串数组Javascript中的字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经定义并填充了一个名为vertices的数组.我可以将输出打印到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. ]

但是我希望从每个元素中删除'v'字符.我已经尝试过使用.replace()函数,如下所示:

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 ', '');
}

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

推荐答案

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

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

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

这篇关于替换字符串数组Javascript中的字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆