在JavaScript的toString添加不同的分隔符()..? [英] Add different delimiters in javascript toString()..?
本文介绍了在JavaScript的toString添加不同的分隔符()..?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
通常JavaScript的的toString()
方法返回数组在这样一个逗号分隔值
normally JavaScript’s toString()
method returns the array in a comma seperated value like this
var myArray = [ 'zero', 'one', 'two', 'three', 'four', 'five' ];
var result = myArray .toString();
和它返回这样的输出 零,一,二,三,四,五
And it returns the output like this zero,one,two,three,four,five
.
不过,我有一个要求重新present结果这种格式的 zero_one_two_three_four_five
(与替换逗号 _
)。
But I have a requirement to represent the result in this format zero_one_two_three_four_five
(replacing the comma with _
).
我知道我们可以使用数组转换为字符串替换后的方法做到这一点。有没有更好的替代方案可用?
I know we can do this using replace method after converting the array to string. Is there a better alternative available?
推荐答案
myArray.join(_)应该做你所需要的。
myArray.join('_') should do what you need.
这篇关于在JavaScript的toString添加不同的分隔符()..?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文