如何在javascript中将数组转换成逗号分隔的字符串[duplicate]

时间:2021-10-05 00:18:13

This question already has an answer here:

这个问题已经有了答案:

I have an array

我有一个数组

a.value = [a,b,c,d,e,f]

一个。值=[a,b,c,d,e,f]

How can I convert to comma seperated string like

如何像这样转换成逗号分隔的字符串

a.value = "a,b,c,d,e,f"

一个。值= " a,b,c,d,e,f”

Thanks for all help.

谢谢你所有的帮助。

3 个解决方案

#1


26  

The method array.toString() actually calls array.join() which result in a string concatenated by commas. ref

方法array.toString()实际上调用array.join(),这会产生由逗号连接的字符串。裁判

var array = ['a','b','c','d','e','f'];
document.write(array.toString()); // "a,b,c,d,e,f"


Array.prototype.join()

The join() method joins all elements of an array into a string.

join()方法将数组的所有元素连接到字符串中。

Arguments:

It accepts a separator as argument, but the default is already a comma ,

它接受分隔符作为参数,但默认情况下它已经是一个逗号,

str = arr.join([separator = ','])

Examples:

var array = ['A', 'B', 'C'];
var myVar1 = array.join();      // 'A,B,C'
var myVar2 = array.join(', ');  // 'A, B, C'
var myVar3 = array.join(' + '); // 'A + B + C'
var myVar4 = array.join('');    // 'ABC'

Note:

If any element of the array is undefined or null , it is treated as an empty string.

如果数组中的任何元素没有定义或为空,则将其视为空字符串。

Browser support:

It is available pretty much everywhere today, since IE 5.5.

自从IE 5.5以后,它现在几乎随处可见。

References

#2


16  

Use the join method from the Array type.

使用数组类型的连接方法。

a.value = [a, b, c, d, e, f];
var stringValueYouWant = a.join();

The join method will return a string that is the concatenation of all the array elements. It will use the first parameter you pass as a separator - if you don't use one, it will use the default separator, which is the comma.

join方法将返回一个字符串,该字符串是所有数组元素的连接。它将使用作为分隔符的第一个参数——如果您不使用它,它将使用默认的分隔符,即逗号。

#3


3  

You can simply use JavaScripts join() function for that. This would simply look like a.value.join(','). The output would be a string though.

您可以简单地使用javascript join()函数。这看起来很像a.valuejoin(',')。但是输出将是一个字符串。

#1


26  

The method array.toString() actually calls array.join() which result in a string concatenated by commas. ref

方法array.toString()实际上调用array.join(),这会产生由逗号连接的字符串。裁判

var array = ['a','b','c','d','e','f'];
document.write(array.toString()); // "a,b,c,d,e,f"


Array.prototype.join()

The join() method joins all elements of an array into a string.

join()方法将数组的所有元素连接到字符串中。

Arguments:

It accepts a separator as argument, but the default is already a comma ,

它接受分隔符作为参数,但默认情况下它已经是一个逗号,

str = arr.join([separator = ','])

Examples:

var array = ['A', 'B', 'C'];
var myVar1 = array.join();      // 'A,B,C'
var myVar2 = array.join(', ');  // 'A, B, C'
var myVar3 = array.join(' + '); // 'A + B + C'
var myVar4 = array.join('');    // 'ABC'

Note:

If any element of the array is undefined or null , it is treated as an empty string.

如果数组中的任何元素没有定义或为空,则将其视为空字符串。

Browser support:

It is available pretty much everywhere today, since IE 5.5.

自从IE 5.5以后,它现在几乎随处可见。

References

#2


16  

Use the join method from the Array type.

使用数组类型的连接方法。

a.value = [a, b, c, d, e, f];
var stringValueYouWant = a.join();

The join method will return a string that is the concatenation of all the array elements. It will use the first parameter you pass as a separator - if you don't use one, it will use the default separator, which is the comma.

join方法将返回一个字符串,该字符串是所有数组元素的连接。它将使用作为分隔符的第一个参数——如果您不使用它,它将使用默认的分隔符,即逗号。

#3


3  

You can simply use JavaScripts join() function for that. This would simply look like a.value.join(','). The output would be a string though.

您可以简单地使用javascript join()函数。这看起来很像a.valuejoin(',')。但是输出将是一个字符串。