0

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

error TS1005: ';' expected.
  let foo:any [] = lst.map (l => {"str": l});
                                       ~
chris01
  • 10,921
  • 9
  • 54
  • 93
  • Does this answer your question? [ECMAScript 6 arrow function that returns an object](https://stackoverflow.com/questions/28770415/ecmascript-6-arrow-function-that-returns-an-object) – jonrsharpe Aug 26 '23 at 08:47

2 Answers2

1

You syntax would not work with Javascript either

You want

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

You need to escape the {} that looks like a function body, and make Javascript think it is dealing with an object

Aron
  • 15,464
  • 3
  • 31
  • 64
0

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})

Balaji Venkatraman
  • 1,228
  • 12
  • 19