Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
256 views
Welcome To Ask or Share your Answers For Others

1 Answer

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" 

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...