Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

122
Vistas
Sort array of object based on nested api call Angular

I have to sort array of object based on array response of first API call. The data should be sorted in ascending order.

Currently I have first api call which returns list of array that will be used in the next api call.

 this.service.fetchStories()
    .pipe(
      take(1),
    ).subscribe((res: any) => {
      this.storyIds = res;
    });

The first call return something like this.

[0001,0002,0003,0004,0005]

And I am looping over the storyIds and passed it in the card component

<div *ngFor="let id of storyIds | slice: start:end">
    <app-cards [id]="id"></app-cards> 
</div>

And I'm fetching the second api based on the ids in my card component

this.service.fetchStoryItems(this.id)
    .pipe(
      take(1)
    )
    .subscribe((res: StoryItem) => {
      if (res !== undefined) {
        this.data = res;
      }
    })

The second api returns each response after the loop

 {name: 'John', score: 1}
 {name: 'Jane', score: 99}
 {name: 'Joe', score: 53}

I'm stuck here and want to sort items based on the score which is returned by the second api call.

I'm thinking something like pushing each object to an array and sort the new array of objects

about 4 years ago · Juan Pablo Isaza
3 Respuestas
Responde la pregunta

0

The best solution would be to make app-cards dumb and provide the already fetched story to app-card.

Component

this.service.fetchStories().pipe(
  switchMap(idArray => forkJoin(idArray.map(id => this.service.fetchStoryItems(this.id)))),
  map(storyArray => storyArray.sort((a,b) => a.score - b.score)),
  take(1),
).subscribe(res => {
  this.stories = res;
});

HTML

<div *ngFor="let story of stories | slice: start:end">
    <app-cards [story]="story"></app-cards> 
</div>

Remove the HTTP calls from app-cards

about 4 years ago · Juan Pablo Isaza Denunciar

0

You can modify the return data from your second api-call

this.service.fetchStoryItems(this.id)
.pipe(
  take(1),
  map(response => {
    return {...response, id: this.id
  })
)
.subscribe((res: StoryItem) => {
  if (res !== undefined) {
    this.data = res;
    console.log(this.data) // should include the id and can be sorted with comparer function
  }
})
about 4 years ago · Juan Pablo Isaza Denunciar

0

How about this.data = res.sort( (a,b) => a.score - b.score );

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda