views:

390

answers:

3

i have a simple array and i want to generate string which include all the elements of the array, for example:

The array is set as follow:

array[0] = uri0
array[1] = uri1
array[2] = uri2

And the output string must be

teststring = uri0,uri1,uri2

I've tried to make this following way (using for loop):

var teststring = "";
teststring = teststring+array[y]

but in the firebug console i see an error message:

"teststring is not defined"

I don't know, what I'm doing wrong. Can someone give me a hint?

+5  A: 
array.join(",")
Glenn
+2  A: 

You must use the join function on the array:

var teststring = array.join(",");
mck89
+1  A: 
array.join();

That is the correct answer. If no value is supplied to the join method a comma is the default element separator. Use the following if you don't want any separator at all:

array.join("");