繁体   English   中英

需要把这个对象放到一个数组中,怎么办?

[英]Need to put this object into an array, how?

我需要使用 javascript 以下面显示的 EXACT 格式转换以下格式,请您建议如何实现这一点

来自: {"healthy":true,"unhealthy_reasons":[]}
到: [{"healthy":true,"unhealthy_reasons":[]}]

如果这就是您需要做的所有事情,您可以将数组括号括在包含对象的变量周围:

let initialObject = {"healthy":true,"unhealthy_reasons":[]};
let arrayedObject = [initialObject];

但我想知道是否还有更多内容。 如果这实际上是更复杂任务的一部分,只需将其添加到您的问题中,您就会得到更完整的答案。

使用JSON.parse()JSON.stringify()

 let data = '{"healthy":true,"unhealthy_reasons":[]}'; let parsed = JSON.parse(data); //TO get an array console.log([parsed]) //TO get a string console.log(JSON.stringify([parsed]))

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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