简体   繁体   English

我应该使用哪个rxjs运算符从可观察的数组中获取值以根据对象属性进行过滤

[英]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.

相关问题 将从rxjs“ from”运算符获得的对象的可观察对象转换为数组的可观察对象 - converting the observable of object obtain from rxjs “from” operator to observable of array Rxjs 发现运算符不能处理 Observable 数组中的值 - Rxjs find operator not working on values in Observable array 我应该使用什么 RxJS 操作从 observable 获取数据并将其推送到数组? - What RxJS operation should I use in getting data from an observable and pushing it to an array? 对于以下情况,我应该使用哪个 rxjs 运算符? - Which rxjs operator should i use for the following condition? 我应该使用哪个 RxJS 运算符来获取第一个真值? - Which RxJS operator should I use for getting the first true value? 我应该从'@ rxjs / Observable'使用`import'rxjs / Rx'`和`import {Observable} - Should I use both `import 'rxjs/Rx'` and `import { Observable } from '@rxjs/Observable'` 为什么过滤器运算符的参数不采用可观察数组中存在的属性 - why the argument from filter operator is not taking a property that exist in the observable array 如何从可观察的 rxjs 中获取两个或多个值作为数组? - How get two or more values as an array from observable rxjs? 按对象属性过滤可观察数组 - Filter observable array by object property Rxjs在数组属性上使用min运算符 - Rxjs use min operator on array property
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM