KingK KingK - 2 months ago 5
Javascript Question

Allowing commas to follow through when converting array to string

I have an array of character with commas separating them. I need to split an array but retain my comma inbetween each character.

See below for an example array:

var myArray = [a,,,b,c,d,,,]


There's a comma in there between the characters "a" and "b". I need to retain the comma when converting the array to a string.

The output string needs to resemble this:

a,bcd,


This is what i'm currently doing to retain the commas:

myArray.toString().replace(/,/g, "");

Answer

How about you use :

var myArray = [a,,,b,c,d,,,];
var str = myArray.join();

This will give a string of array elements, preserving the commas.

Comments