简体   繁体   中英

Assign array to JSON object as a new object

I have an array which includes some values, ex.

let array = [value1, value2];

How can I create a JSON object like the following?

{
  "field": "[value1, value2]"
}

JSON.stringify for output a json, ref: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify

I think you could find it pretty easily with a Google search :)

 const result = JSON.stringify({ field: ["toto", "tutu"]}) console.log(result)

Use JSON.stringify() method:

let array = [value1, value2];
let yourJObject={"field":JSON.stringify(array)}

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