Reputation: 234
I work with Typescript and angular 4. I have 2 list (rxjs/Rx)
...
myList: Observable<MyClass[]>;
filteredList: Observable<MyClass[]>;
...
I want filtered myList and push result in the filteredList by looking at the property string of MyClass is equal to '123' or another string The filteredlist is mapped on a autcomplete angular and I have this code
this.filteredList= this.myForm.get("myautocomplete").valueChanges .startWith(null) .map(???);
What's the best way to resolve this problem?
I can not do it !
Thanks for your help
Upvotes: 0
Views: 1742
Reputation: 41274
This should do it:
myList: Observable<MyClass[]>;
filteredList = myList.map(items =>
items.filter(item => item.string === '123'));
Autocomplete example:
filteredList = myForm.get("autocomplete").valueChanges
.startWith('')
.combineLatest(myList)
.map(([q, items]) => items.filter(item => item.string === q));
Upvotes: 3
Reputation: 11203
You have two observables.
You want to take first one and transform it to a bit. You can use methid .map
for it.
filteredList = myList.map(arr => arr.filter(x => x['stringPropOfMyClass'] === '123'));
I suggest you first get the basics of Observables down.
Upvotes: 0
Reputation: 125
Well, since typescript is based on JS, so if you want to know if there's a method to do this or that, go check it out.
You want to use .filter
on an array :
-From MDN :
The filter() method creates a new array with all elements that pass the test implemented by the provided function.
So if you want to filter the number 0 on the array [0,1,2,3] named ar
for instance :
filteredAr = ar.filter(entry => entry !== 0);
But since its an observable, you have to subscribe to its changes and change filteredList
type...
Upvotes: 1