[英]Which rxjs operator should I use to get values from an observable array to filter based on an object property
I have been racking my brain for a couple of weeks now with this problem. 我已经为这个问题绞尽脑汁了几个星期。 I have typed observable array being returned via an http request.
我键入了通过http请求返回的可观察数组。 This the array output to the console.
这会将数组输出到控制台。 I am trying to grab each item and check the properties._hg_layer.
我试图抓住每个项目并检查properties._hg_layer。
An ngrx state selector is then assigned to Observable and then passed to a BehaviorSubject. 然后将ngrx状态选择器分配给Observable,然后传递给BehaviorSubject。 This emits the array but won't let me get out the values inside of the array.
这会发出数组,但不会让我得出数组内部的值。 I have tried every conceivable operator combination.
我尝试了每种可能的运算符组合。 I can't tell if my issue has to do with how the response is typed coming out of the service or how I am using the operators.
我无法确定我的问题是否与如何从服务中键入响应或我如何使用运算符有关。 It will not let me have access to the typed members of the array.
它不会让我访问数组的类型化成员。 IT's as if it doesn't know the properties of the array objects.
好像它不知道数组对象的属性一样。
I have tried assigning the type of object on the subscription and I can't get that to work either. 我尝试在订阅上分配对象的类型,但我也无法使它正常工作。
Any insight or help you could provide would be greatly appreciated. 您提供的任何见解或帮助将不胜感激。 Thank you very much.
非常感谢你。
Try this: 尝试这个:
this.featureSource.subscribe((features) => {
// iterate here
features.map((feature) => console.log(feature.properties));
// or good ol'
for (const feature of features) {
console.log(feature.properties);
}
})
Also, need to keep in mind that your take(2)
will actually emit the features independently , not as an array. 另外,请记住,
take(2)
实际上将独立发出功能 ,而不是作为数组发出 。 If you were to continue to use that, the code snippet above will need to change to process the item (essentially just doing what you would do in a map). 如果要继续使用该代码,则需要更改上面的代码片段以处理该项目(本质上就是按照您在地图中所做的操作)。
In the end it was getting the order of the mapping correct. 最后,它使映射顺序正确。 I had to map the array in the initial map().
我必须在初始map()中映射数组。 Then map the individual objects on the subscription.
然后将各个对象映射到订阅上。 I solved my problem with the following:
我通过以下方法解决了我的问题:
this.updatedLayers$.pipe(
map((features: Feature[]) => features),
)
.subscribe(val => {
setTimeout(() => {
featureSubject.next(val.length);
this.featureSource.next(val);
console.log('feature data', val);
val.map(feature => {
console.log('type', feature.type);
console.log('properties', feature.properties._hg_layer);
});
}, 500);
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.