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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 07:11:04  来源:igfitidea点击:

using .join method to convert array to string without commas

javascriptjquery

提问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/

JS小提琴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('');

http://jsfiddle.net/mowglisanu/CVr25/1/

http://jsfiddle.net/mowglisanu/CVr25/1/

回答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("");

回答by adeneo

All you need to do is :

您需要做的就是:

arr.join('');

FIDDLE

小提琴