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

349
Views
Angular 12 how to return an http get request array of objects and assign to other array of different type using map()

Hey I'm trying to implement a bootstrap5 dropdown following this example: Creating Multi-Select Dropdown with Angular and Bootstrap 5 In that example, to get the data, he uses an app.service and just returns an array of objects:

getFoods(): Food[] {
        return [
            {
                id: 1,
                name: 'Grapes'
            },
            {
                id: 2,
                name: 'Melon'
            },
...

And then in his ngOnInit() calls the getFoods() method and also uses .map() operator because he has to assign to values because the item model has two values:

ngOnInit(): void {
        this.items = this.appService.getFoods().map(fruit => ({
            id: fruit.id,
            name: fruit.name
        } as Item));
    }

So I'm trying to do hat but with data being fetched from an API endpoint using HTTP GET request. But I don't know how to use the .map() operator for the http get request:

this.subscription = this.contactSearchService.currentCodes.pipe(
            map(
                code => (
                    {
                        id: code.code,
                        name: code.code
                    }
                )
            )).subscribe(Response => {
                this.items = Response
            })

It's giving me these errors:

Property 'code' does not exist on type 'ResponsibilityCode[]'.
Type '{ id: any; name: any; }' is missing the following properties from type 'Item[]': length, pop, push, concat, and 26 more.

My http get request function:

private _reponsibilityCodeSource = new BehaviorSubject<ResponsibilityCode[]>([]);
currentCodes = this._reponsibilityCodeSource.asObservable();

getCodes(): void {
    this.http.get<ResponsibilityCode[]>('https://localhost:44316/api/SITEContacts/ResponsibilityCode').subscribe(Response => {
        this._reponsibilityCodeSource.next(Response);
      });
  }

I get the data as `JSON` btw.

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

The rxjs pipe(map(.... code...)) is different than array.map - pipe(map()) does not operate on each item of an array

So the errors you are getting is because you're swapping out the array of ResponsibilityCode for a single item (code in your code is all the responsibility codes)

Try

this.subscription = this.contactSearchService.currentCodes.subscribe(Response => {
                this.items = Response.map(
                code => (
                    {
                        id: code.code,
                        name: code.code
                    }
                )
            )
})
about 4 years ago · Juan Pablo Isaza Report

0

Your HTTP get returns an Observable of ResponsibilityCode array, so to achieve that you have to map (Array.prototype.map) the items of the array within the RxJS's map operator, like the following:

this.subscription = this.contactSearchService.currentCodes
  .pipe(
    map((res: ResponsibilityCode[]) =>
      res.map((item) => ({
        id: item.id,
        name: item.name,
      }))
    )
  )
  .subscribe((res) => {
    this.items = res;
  });
about 4 years ago · Juan Pablo Isaza Report
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!