Reputation: 7421
I'm looking to find a neat way to create a comma-delimited string from an array. This is how I'm doing it now...
for(i=0;i<10;i++)
{
str = str + ',' + arr[i];
}
str=str.substring(1)
return str;
... but it feels a bit untidy.
Upvotes: 41
Views: 50713
Reputation: 55489
I think there is something like array.join(',')
where array is your array variable instance.
Upvotes: 1
Reputation: 236022
Array.prototype.join()
is what you're looking for:
arr.join(',');
https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Array/join
var arr = ['Hi', 'I', 'am', 'a', 'comma', 'separated', 'list'];
arr.join(','); // === "Hi,I,am,a,comma,separated,list"
Upvotes: 72