0

I have a .net service returning a dto containing a property of type dictionary & I would like to desirialize it in Angular into a dto containing a property of type Map.

I tried to achieve this by simply defining the type of the property of the dto as a Map & it does transpile but fails in runtime when I try to use Map methods such as ForEach() as the runtime type of the property is Object & not Map as I defined it.

Is there a way to achieve this without creating another type & mapping it entry by entry?

  • Note https://angular.io/guide/http#requesting-a-typed-response and https://github.com/angular/angular/issues/25401 - specifying the generic type **does not** involve any validating or casting. What you actually get will be the result of `JSON.parse` which is going to be vanilla objects. If you want a Map you do need to create than explicitly - see https://stackoverflow.com/q/36644438/3001761 – jonrsharpe Jun 04 '20 at 14:27
  • Does this answer your question? [How to convert a plain object into an ES6 Map?](https://stackoverflow.com/questions/36644438/how-to-convert-a-plain-object-into-an-es6-map) – jonrsharpe Jun 04 '20 at 14:45

1 Answers1

0

Lets say your object is :-

public abc = {
   'name': 'Aakash',
   'country: 'India'
}

//To convert into map
public abcMap = new Map(Object.entries(this.abc));
Aakash Garg
  • 10,649
  • 2
  • 7
  • 25