0

I need to convert the following code into TypeScript. it is to create a key\value pairs

this.charUrls = item.urls.reduce(function (map, obj) {
  map[obj.type] = obj.url;
  return map;
}, {});

I tried the following but it didn't work

this.charUrls = item.urls.reduce(function (map, obj) { map[obj.type] = obj.url; return map; });
rrk
  • 15,677
  • 4
  • 29
  • 45
Looking4Help
  • 32
  • 1
  • 4

1 Answers1

0

I think you have forgotten to define item variable. Try this,

let item:any;
this.charUrls = item.urls.reduce(function (map, obj) { map[obj.type] = obj.url; return map; });

Now it doesn't show any syntax errors in my IDE.

However, with further research I found out that ES6 Map is not supported by TypeScript natively now.

Refer this great answer thread, ES6 Map in Typescript

heatstreak
  • 121
  • 3