简体   繁体   中英

convert array to json in typescript

im trying to convert an array to json in typeScript, how can i do it to get this result please:

 let array=['element1', 'element2', 'element3']

result=[{"value"="element1"},{"value"="element2"}, {"value"="element3"}]

To be clear, the JSON version of your array would be exactly that:

['element1', 'element2', 'element3']

If you want to add the value field, you can manually add it first, before converting into JSON.

Working demo

The below code produces this:

[{"value":"element1"},{"value":"element2"},{"value":"element3"}]

 let array = ['element1', 'element2', 'element3'] let arrayWithValue = array.map(el => ({value: el})); console.log(JSON.stringify(arrayWithValue));

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