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

236
Visualizações
Modify array in observable with rxjs, return whole object

I have observable like this:

currentPage: 1
items: (6) [{…}, {…}, {…}, {…}, {…}, {…}]
pageSize: 10
totalItems: 6
totalPages: 1

I'm trying to modify every element in items array, and then return a whole object.

   getAllGifts(): Observable<PagedListDTO<GiftIdeaDTO>> {
    return this.http.get<PagedListDTO<GiftIdeaDTO>>(this.urlAPI + '/GiftIdeas/GetAll').
      pipe(
        map(x => x.items.map
          (y => ({ ...y, imageContentB64: y.imageContentB64 + 'Bob' })))
        ,
        tap(console.log)
      );
  }

but i only get a modified array of items, no currentPage property, page Size etc.

How can i modify items array and return whole object?

about 4 years ago · Juan Pablo Isaza
3 Respostas
Responde à pergunta

0

You seem to be already familiar with the usage of spread syntax (...). You could also use it for the outer object before applying the Array#map to the items property.

Try the following

getAllGifts(): Observable<PagedListDTO<GiftIdeaDTO>> {
  return this.http
    .get<PagedListDTO<GiftIdeaDTO>>(this.urlAPI + '/GiftIdeas/GetAll')
    .pipe(
      map(ideas => ({
        ...ideas,                          // retain properties of the outer object
        items: ideas.items.map(item => ({  // adjust the `items` property
          ...item, 
          imageContentB64: item.imageContentB64 + 'Bob' 
        }))
      })),
      tap(console.log)
    );
}
about 4 years ago · Juan Pablo Isaza Relatório

0

The map(x => only accounts for x.items and misses the rest of the props.

This should fix it:

   getAllGifts(): Observable<PagedListDTO<GiftIdeaDTO>> {
    return this.http.get<PagedListDTO<GiftIdeaDTO>>(this.urlAPI + '/GiftIdeas/GetAll').
      pipe(
        map(x => ({...x, items: x.items.map
          (y => ({ ...y, imageContentB64: y.imageContentB64 + 'Bob' })))
        }),
        tap(console.log)
      );
  }

In the above code, x is mapped to include all props and then items is updated using x.items.map.

about 4 years ago · Juan Pablo Isaza Relatório

0

You are not returing them, use this:

getAllGifts(): Observable<PagedListDTO<GiftIdeaDTO>> {
return this.http.get<PagedListDTO<GiftIdeaDTO>>(this.urlAPI + '/GiftIdeas/GetAll').
    pipe(
        map(x => {
            return {
                ...x,
                items: x.items.map(y => ({ ...y, imageContentB64: y.imageContentB64 + 'Bob' }))
            }
        })
    )
    ,tap(console.log)
    );
}
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