I have a array of strings:
str[1]='apple';
str[2]='orange';
str[3]='banana';
//...many of these items
Then, I would like to construct a string variable which looks like var mystr='apple,orange,banana,...', I tried the following way:
var mystr='';
for(var i=0; i<str.length; i++){
mystr=mystr+","+str[i];
}
Which is of course not what I want, is there any efficient way to connect all this str[i] with comma?
just use the built-in join function.
str.join(',');
Check out join function
var str = [];
str[0]='apple';
str[1]='orange';
str[2]='banana';
console.log(str.join(','));
would output:
apple,orange,banana
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With