[英]Filter Observables before subscribing angular2
I have data like the one below in my mongoDB and I retrieve calling the service and later subscribing to it. 我的mongoDB中有类似下面的数据,我检索了调用服务并随后订阅该服务。 But I want to filter the before subcribing to it in a way that i just subscribe to the filtered data base on my condition.
但是我想在订阅它之前过滤,以便我只根据自己的条件订阅过滤后的数据库。 I want to filtered verifying that one of the camps in the data from the backend matches the "this.idcalbuscador".
我想过滤验证后端数据中的一个营地是否匹配“ this.idcalbuscador”。 (See the .filter()).
(请参阅.filter())。 I haven't been able to achieve this, could anyone help?
我无法实现这一目标,有人可以帮忙吗? Just new to observables
刚开始是可观察的
Data 数据
{
"_id" : ObjectId("588850b746f8ce140c1fe8cf"),
"idpertenalcalendario" : ObjectId("5885bfe452c6ba1d50f37f19"),
"enabled" : true,
"forma" : "Rombo",
"color" : "Red",
"type" : "point",
"descrip" : "ghghghgh",
"startDate" : "2017-01-15",
"nameHito" : "gjgjgjgg",
}
Code 码
//retrieve the datas
this._hitoService.getHitos()
.filter(resp => resp.idpertenalcalendario === this.idcalbuscador )
.subscribe(hito1 =>{
if (typeof this.nom_cal1 === "undefined"){
swal("Atencion!", "Busca un calendario con el buscador del Side Menu")
}else {
drawtimeline1_1(this.hito1, this.nom_cal1);
}
});
The filter
function is not called for every item in your array, like it is for instance in Java's stream API. 并非针对数组中的每个项目都调用
filter
函数,就像Java的流API中那样。 It is called for a single emittance of the Observable. 它被称为Observable的单个发射。 That means the
resp
you work with in the filter function contains the array and not a single hito
. 这意味着您在filter函数中使用的
resp
包含数组而不是单个hito
。 That's why the following comparison will always return false: 这就是为什么以下比较将始终返回false的原因:
resp.idpertenalcalendario === this.idcalbuscador
This is, at least, what I'd expect according to the names: getHitos
returns an array of Hito
. 至少这是我根据名称期望的结果:
getHitos
返回一个Hito
数组。 But Pierre Duc is right, it depends on your actual implementation. 但是Pierre Duc是正确的,这取决于您的实际实现。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.