简体   繁体   English

将每个数字转换为字符串并将它们连接起来

[英]To convert each number to string and concatenate them

I have javascript array of integers:我有 javascript 整数数组:

[1,3,2,9,5,44]

I need to convert each number to string and concatenate them:我需要将每个数字转换为字符串并将它们连接起来:

"1,3,2,9,5,44"

How can I implement it ?我该如何实施?

使用数组的join方法:

[1,3,2,9,5,44].join(',');
 var array = [1,3,2,9,5,44];
    var result = array.join(",");

You could use JavaScript Array join() method like this:你可以像这样使用 JavaScript Array join()方法:

var arrayOfIntegers = [1,3,2,9,5,44];
var concatenated = arrayOfIntegers.join();

Note that the default separator for this method is comma, so the result would be请注意,此方法的默认分隔符是逗号,因此结果将是

1,3,2,9,5,44 1、3、2、9、5、44

If you want to have the result string some other way, you could pass the separator as an argument to this method, eg如果您想以其他方式获得结果字符串,您可以将分隔符作为参数传递给此方法,例如

var arrayOfIntegers = [1,3,2,9,5,44];
var concatenated = arrayOfIntegers.join(" , ");

would produce this string会产生这个字符串

1 , 3 , 2 , 9 , 5 , 44 1、3、2、9、5、44

http://www.w3schools.com/jsref/jsref_join.asp http://www.w3schools.com/jsref/jsref_join.asp

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM