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

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.

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));

Comments

1

Use JSON.stringify

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

Comments

0

Try

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

This should take care of the sub-arrays.

Comments

Your Answer

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.