Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

convert array to json in typescript

i m 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"}]

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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));
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading