我有這樣的觀察:
currentPage: 1
items: (6) [{…}, {…}, {…}, {…}, {…}, {…}]
pageSize: 10
totalItems: 6
totalPages: 1
我正在嘗試修改 items 陣列中的每個元素,然后回傳整個物件。
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)
);
}
但我只得到一個修改過的專案陣列,沒有 currentPage 屬性、頁面大小等。
如何修改專案陣列并回傳整個物件?
uj5u.com熱心網友回復:
唯一的map(x =>占x.items和錯過了其余的props。
這應該解決它:
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)
);
}
在上面的代碼中,x被映射為包含所有道具,然后items使用x.items.map.
uj5u.com熱心網友回復:
您不會退回它們,請使用以下命令:
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)
);
}
uj5u.com熱心網友回復:
您似乎已經熟悉展開語法 ( ...)的用法。Array#map在將應用于屬性之前,您也可以將其用于外部物件items。
嘗試以下
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)
);
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/419636.html
標籤:
