I want to convert an array to a sting so that visually it is just the same but with quote marks around it.
For example:
array= [1,2,3,[4,5,6],[7,8,9]]
goes to => "[1,2,3,[4,5,6],[7,8,9]]"
I tried String(array) but this just punches through all the brackets to give me:
"1,2,3,4,5,6,7,8,9"
Anyone got any ideas on this?
Thanks
4 Answers 4
You could use JSON.stringify
JSON.stringify(array);
The JSON.stringify() method converts a JavaScript value to a JSON string, optionally replacing values if a replacer function is specified, or optionally including only the specified properties if a replacer array is specified.
Comments
Use JSON.stringify
array= [1,2,3,[4,5,6],[7,8,9]]
console.log(JSON.stringify(array));
Comments
Use JSON.stringify
console.log(JSON.stringify([1,2,3,[4,5,6],[7,8,9]]));
Comments
Try
array = array.join(",");
array = array.split(",");
array = array.join("");
This should take care of the sub-arrays.