1

I have an javascript array of string that looks like:

A = ['a', 'b', 'c'];

I want to convert it to (all string):

strA = '"a","b","c"'

How do I achieve this?

Tushar
  • 85,780
  • 21
  • 159
  • 179
return 0
  • 4,226
  • 6
  • 47
  • 72

4 Answers4

4

You can use join with "," as glue.

var strA = '"' + A.join('","') + '"';

join will only add the glue between the array elements. So, you've to add the quotes to start and end of it.

Tushar
  • 85,780
  • 21
  • 159
  • 179
1

Try this

A = ['a', 'b', 'c'];
A.toString();
alert(A);
Antony
  • 171
  • 2
  • 11
0

You could try just concatenating the values in a simple for loop something like this:

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);
jmancherje
  • 6,439
  • 8
  • 36
  • 56
0

Do you mean something like this? This works in 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"]);
goodfriend0
  • 193
  • 1
  • 8