[英]Why can't i chain the .map() function on observables
我很難理解為什么我不能在get請求的回調上鏈接.map()函數。
const random = max => Math.floor((Math.random() * max) + 1);
this._http.get('https://unsplash.it/list')
.map(response => response.json())
.map(metaImages => metaImages.url_post)
.subscribe(urls => {
this.imgSrc = urls[random(urls.length)];
});
所以我把它分解成這樣
const random = max => Math.floor((Math.random() * max) + 1);
this._http.get('https://unsplash.it/list')
.map(response => response.json())
.subscribe(metaImages => {
const urls = metaImages.map(url => url.post_url);
this.imgSrc = urls[random(urls.length)];
});
但是,為什么呢? 感謝您的幫助。
確保不要將映射可觀察對象(映射可觀察發射流)與映射數組(映射數組中的元素)混淆。 在您的情況下,如果要在訂閱前在可觀察級別進行映射,則可觀察映射必須對發射進行數組映射。
const random = max => Math.floor((Math.random() * max) + 1);
this._http.get('https://unsplash.it/list')
.map(response => response.json())
.map(metaImages => metaImages.map(url => url.post_url))
^^^ MAP EMITTED ARRAY ^^^ MAP ELTS IN EMITTED ARRAY
.subscribe(urls => this.imgSrc = urls[random(urls.length)]);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.