angular2 observables过滤每个数组元素,而不是将其返回到数组

时间:2016-04-10 08:13:38

标签: arrays angular rxjs observable

我有一个带ID,标题的线程对象数组和一个isBookmarked:boolean。

我创建了一个Subject,我想订阅它以获得一个isBookmarked = true的Thread对象数组。

https://plnkr.co/edit/IFGiM8KoSYW6G0kjGDdY?p=preview

我在服务中

export class Service {

  threadlist:Thread[] = [
    new Thread(1,'Thread 1',false),
    new Thread(2,'Thread 2',true),
    new Thread(3,'Thread 3',false),
    new Thread(4,'Thread 4',true),
    new Thread(5,'Thread 5',true),
    new Thread(6,'Thread 6',false),
    new Thread(7,'Thread 7',false),
    ]

  threadlist$:Subject<Thread[]> = new Subject<Thread[]>()

 update() {
   this.threadlist$.next(this.threadlist)
 }

}
组件中的

export class AppComponent implements OnInit {
 localThreadlist:Thread[];
 localThreadlistFiltered:Thread[];
 constructor(private _service:Service){}

 ngOnInit():any{
  //This updates the view with the full list
  this._service.threadlist$.subscribe( threadlist => {
    this.localThreadlist = threadlist;
  })
  //here only if isBookmarked = true
  this._service.threadlist$
    .from(threadlist)//????
    .filter(thread => thread.isBookmarked == true)
    .toArray()
    .subscribe( threadlist => {
      this.localThreadlistFiltered = threadlist;
    })
  }
  update() {
    this._service.update();
  }
}

我通常使用哪种实例方法来拆分数组? 还有更好的方法吗?

由于

1 个答案:

答案 0 :(得分:1)

您可以在observables的 <app-deployment> <name>jms-rename</name> <target>DefaultServer</target> <module-type>ear</module-type> <source-path>C:\JMSTest\application\jms\target\jms-0.0.1-SNAPSHOT.ear</source-path> <security-dd-model>DDOnly</security-dd-model> </app-deployment> 运算符中使用JavaScript数组的filter方法:

map

请参阅此plunkr:https://plnkr.co/edit/COaal3rLHnLJX4QmvkqC?p=preview