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

How can I map a list to a list of JSON?

I try to cast a list of strings [x, y, z] to
a list of JSON-objects [{str:x},{str:y},{str:z}].

Thats my try, but I get an error.

let lst:string [] = ["a", "b", "c"];

let foo:any [] = lst.map (l => {"str": l});

Error

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

error TS1005: ';' expected.
  let foo:any [] = lst.map (l => {"str": l});
                                       ~

>Solution :

While returing you need to use brackets(). Because we are inside a loop {} will be treated as block. So the updated code will be,

let lst: string[] = ["a", "b", "c"];

let foo: any[] = lst.map(l => ({ "str": l }));

Note ({"str": l})

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