1

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

asked Jul 15, 2015 at 15:59

4 Answers 4

1

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.

answered Jul 15, 2015 at 16:02
Sign up to request clarification or add additional context in comments.

Comments

1

Use JSON.stringify

array= [1,2,3,[4,5,6],[7,8,9]]
console.log(JSON.stringify(array));

answered Jul 15, 2015 at 16:03

Comments

1

Use JSON.stringify

console.log(JSON.stringify([1,2,3,[4,5,6],[7,8,9]]));

answered Jul 15, 2015 at 16:03

Comments

0

Try

array = array.join(",");
array = array.split(",");
array = array.join("");

This should take care of the sub-arrays.

answered Jul 15, 2015 at 16:08

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.