Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

125
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório

0

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

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda