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/
JS小提琴http://jsfiddle.net/CVr25/
4 个解决方案
#1
187
Simply like that:
只是像这样:
arr.join("")
#2
25
You can specify an empty string as an argument to join, if no argument is specified a comma is used.
您可以指定一个空字符串作为要连接的参数,如果没有指定任何参数,则使用逗号。
arr.join('');
http://jsfiddle.net/mowglisanu/CVr25/1/
http://jsfiddle.net/mowglisanu/CVr25/1/
#3
16
The .join()
method has a parameter for the separator string. If you want it to be empty instead of the default comma, use
.join()方法具有分隔符字符串的参数。如果您希望它是空的,而不是默认的逗号,请使用
arr.join("");
#1
187
Simply like that:
只是像这样:
arr.join("")
#2
25
You can specify an empty string as an argument to join, if no argument is specified a comma is used.
您可以指定一个空字符串作为要连接的参数,如果没有指定任何参数,则使用逗号。
arr.join('');
http://jsfiddle.net/mowglisanu/CVr25/1/
http://jsfiddle.net/mowglisanu/CVr25/1/
#3
16
The .join()
method has a parameter for the separator string. If you want it to be empty instead of the default comma, use
.join()方法具有分隔符字符串的参数。如果您希望它是空的,而不是默认的逗号,请使用
arr.join("");