简体   繁体   中英

Return an array as a string JavaScript

I want return an array as a string in my function.
Example:

return [[1],[1,1],[1,2,1],[1,3,3,1],[1,4,6,4,1]]

should be returned as "[[1],[1,1],[1,2,1],[1,3,3,1],[1,4,6,4,1]]" which is a string. I don't want to use .join() because it removes the [] brackets.

您可以使用JSON.stringify将JS对象转换为JSON字符串。

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

Return your array after JSON.stringify it. like that:

var arr = [[1],[1,1],[1,2,1],[1,3,3,1],[1,4,6,4,1]];
return JSON.stringify(arr);

It will convert the array object into string.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM