Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

235
Views
Angular - Make Service return ES6 Map

I'm struggling with the ES6 Map Datatype in my HTTP-Service.

In my Service I return an Observable, containing a Map:

  getLayout(dims: Map<number, [number, number]>, rels: [number, number][]): Observable<Map<number, [number, number]>> {
    // ES6 Map can not be json-stringified
    let jsonDims: { [x: number]: [number, number]; } = {}
    dims.forEach((dim, gid) => {
      jsonDims[gid] = dim
    });
    const data = { "dims": jsonDims, "rels": rels }

    return this.http.post<Map<number, [number, number]>>(
      environment.backend_url + '/layout',
      JSON.stringify(data),
      this.options
    ).pipe(catchError(err => {
        console.error(err.message)
        return throwError("Error occurred while requesting layout.")
    }));
  }

However, when I try to process the response-data in my component:

this.layoutService.getLayout(dims, rels).subscribe(layout => {
  console.log(layout); // works: I see the data, structured correctly

  for (const [gid, pos] of layout) { // fails
    console.log(gid)
    console.log(pos)
  }
});

I get error messages like:

layout not iterable

layout has no attribute 'get'

So it seems layout is not a valid Map, so how would I have http.post<Map<number, [number, number]>>() actually return a Map-object over which I can iterate or use get()?

about 4 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!