Click here to Skip to main content
15,867,686 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi everyone,
I would want to convert an array list to a comma separated string in javascript..
Size of arraylist is not known but its finite.
How can I achieve this?
Thanks in advance.
Posted
Comments
Anurag Sinha V 9-Oct-13 7:25am    
Are you sure you have an arraylist??Or is it an array?
Member 10325302 9-Oct-13 7:27am    
Yes,its an arraylist.
Anurag Sinha V 9-Oct-13 7:29am    
Can you post the code as to what do you have?

1 solution

Maybe:
JavaScript
function ArrayListToCommaSeparatedString(arraylist) {
   string result = arraylist[0];
   for (int i = 1; i < arraylist.Length; i++) {
      result += "," + arraylist[i];
   }
   return result;
}
 
Share this answer
 
Comments
Anurag Sinha V 9-Oct-13 7:30am    
Hi Phil.O, Can you explain the logic behind your solution? I have some confusions.
phil.o 9-Oct-13 7:32am    
What confusions? The code is pretty much self-explanatory.
Anurag Sinha V 9-Oct-13 7:36am    
you result variable will store the first array of the arrayList right?
If yes then shouldn't the condition be changed to i < arrayList[0].Length
phil.o 9-Oct-13 7:44am    
No, in my opinion it should not.
But you are free to test both versions and see what different results you get :)
Anurag Sinha V 9-Oct-13 12:01pm    
sure..will do so...thank you

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900