Javascript 使用 .join 方法将数组转换为不带逗号的字符串
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12132178/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
using .join method to convert array to string without commas
提问by styler
Possible Duplicate:
array join() method without a separator
可能的重复:
没有分隔符的数组 join() 方法
I'm using .join()
to convert my array to a string so I can output it in a text box as the user selects numbers in a calculator, I'm not entirely sure how I can remove the commas that are also being output in the list however. Can someone advise how this can be achieved or if there is a different approach I should be using?
我正在使用.join()
将我的数组转换为字符串,以便我可以在用户在计算器中选择数字时将其输出到文本框中,但我不完全确定如何删除也在列表中输出的逗号. 有人可以建议如何实现这一点,或者我是否应该使用不同的方法?
JS
JS
$(document).ready(function() {
var total = 0;
var arr = [];
//Testing
$('#calculator').children('.num').on('click', function(e) {
var clickedNumber = $(this).data('id');
arr.push(clickedNumber);
console.log(arr.join());
e.preventDefault();
});
});?
JS Fiddle http://jsfiddle.net/CVr25/
回答by VisioN
Simply like that:
就像这样:
arr.join("")
回答by Musa
You can specify an empty string as an argument to join, if no argument is specified a comma is used.
您可以指定一个空字符串作为要加入的参数,如果未指定参数,则使用逗号。
arr.join('');
回答by Bergi
The .join()
methodhas a parameter for the separator string. If you want it to be empty instead of the default comma, use
该.join()
方法有一个用于分隔符字符串的参数。如果您希望它为空而不是默认逗号,请使用
arr.join("");