我有多個父對象,每個都有多個子對象,每個子對象都有多個鏈接。我想檢查所有的兒童鏈接,刪除沒有實時鏈接的兒童,並刪除所有孩子已被刪除的父母。如何鏈接多個可觀察的減少角度4以生成base64圖像
我想做了解如何編寫角4使用的Rx下面的代碼:
parents.reduce((updatedParents, parent) => {
parent.children = parent.children.reduce((updatedChildern, child) => {
child.links = child.links.reduce((updatedLinks, link) => {
if (link is live) updatedLinks.push(link) // asnyc operation
return updatedLinks;
},[]);
if (child.links.length) updatedChildren.push(child);
},[]);
if (parent.children.length) updatedParents.push(parent);
},[])
我能想象如何做到這一點使用的承諾,並承諾所有/ promise.reduce等與藍鳥,但我怎麼去實現這個使用Observable並減少等?我真的很困惑,我不知道該怎麼做。我嘗試了幾種方法,無法實現它的功能。我希望單個事件能夠向我返回過濾結果,就像承諾一樣。
任何建議將受到歡迎。
這是結構:
const parents = [
{
children: [
{
links: [
'http://www.onelink.com', 'http://www.secondlink'
]
},
{
links: [
'http://www.threelink.com', 'http://www.fourlink'
]
}
]
},
{
children: [
{
links: [
'http://www.fivelink.com', 'http://www.sixlink'
]
},
{
links: [
'http://www.sevenlink.com', 'http://www.egihtlink'
]
}
]
}
這是我走多遠。
return Observable.from(parents)
.flatMap(parent => Observable.from(parent.children)
.flatMap(child => Observable.from(child.images)
.reduce((updatedImages, imgSrc, idx) => {
return this.http.get(imgSrc)
.map(res => {
if (res.status === 200) {
updatesImages.push(base64Ecode(imgSrc));
}
console.log(res);
console.log(updatedImages);
return updatedImages;
});
})
.filter(p => p.length !== 0)
)
.filter(c => c.length !== 0)
updateLinks是obervable? RXjs有一個reduce操作符,但它用在observable上,而不是通過數組reduce發送observable。 – rjustin
目前還不清楚你在問什麼。輸入數據很好理解。您試圖實現的數據轉換是什麼,以及可能需要的副作用是什麼? –
@Dory Zidon你的意思是你只是想對孩子的所有鏈接執行異步操作(如果有的話)? – CozyAzure