[英]javascript: How to convert a array of string into a pure string that concatenate the items separated by comma?
I have an javascript array of string that looks like: 我有一个字符串的javascript数组,看起来像:
A = ['a', 'b', 'c'];
I want to convert it to (all string): 我想将其转换为(所有字符串):
strA = '"a","b","c"'
How do I achieve this? 我该如何实现?
You can use join
with ","
as glue. 您可以使用带有","
join
作为胶水。
var strA = '"' + A.join('","') + '"';
join
will only add the glue between the array elements. join
只会在数组元素之间添加胶水 。 So, you've to add the quotes to start and end of it. 因此,您必须在引号的开头和结尾添加引号。
Try this 尝试这个
A = ['a', 'b', 'c'];
A.toString();
alert(A);
You could try just concatenating the values in a simple for loop something like this: 您可以尝试在一个简单的for循环中串联这些值,如下所示:
var array = ["1", "a", 'b'];
var str = '';
for (var i = 0; i<array.length; i++){
str += '"' + array[i] + '",';
}
str = str.substring(0, str.length - 1);
or if you're feeling crazy you could do : 或者,如果您感到疯狂,可以执行以下操作:
str = JSON.stringify(array)
str = str.substring(1, str.length -1);
Do you mean something like this? 你的意思是这样吗? This works in chrome. 这适用于Chrome。
function transformArray(ar) {
var s = ar.map(function (i) {
return "\"" + i + "\","; })
.reduce(function(acc, i) {
return acc + i;
});
return s.substring(0, s.length-1);
}
transformArray(["a", "b", "c"]);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.