开发者

Create comma-delimited string

I'm looking to find a neat way to create a comma-delimited string from an array. This is how I'm doing it no开发者_开发技巧w...

for(i=0;i<10;i++)
{
   str = str + ',' + arr[i];
}
str=str.substring(1)
return str;

... but it feels a bit untidy.


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" 


Use the join method:

arr.join(',');


you have to use

var joinedstr = myarray.join(',');


I think there is something like array.join(',') where array is your array variable instance.


Use the join function:

myarray.join(',');

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜