我有多个父对象,每个都有多个子对象,每个子对象都有多个链接。我想检查所有的儿童链接,删除没有实时链接的儿童,并删除所有孩子已被删除的父母。如何链接多个可观察的减少角度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