当前位置: 动力学知识库 > 问答 > 编程问答 >

Javascript, convert an array to a string like so

问题描述:

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

网友答案:

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.

网友答案:

Use JSON.stringify

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

Use JSON.stringify

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

Try

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

This should take care of the sub-arrays.

分享给朋友:
您可能感兴趣的文章:
随机阅读: