Reputation: 6289
In one view I am pulling an array of client records from our mongoDB/Node-based API and printing them to the screen in my Angular app. Now I want to create another view where a filtered list of those records are returned.
Specifically I want to return a list of client records that have a value on the object for the property "exitDate". The idea here is that if there IS an exitDate (i.e. the value is not null), then this means the record is an "inactive" result.
The original collection of records are being generated via an observable in the OnInit life cycle, like this:
this.clientService.getAll()
.subscribe(resRecordsData => this.clients = resRecordsData,
responseRecordsError => this.errorMsg = responseRecordsError);
So to do this I'm trying to use a filter function. This is what I have:
isInactive() {
this.inactiveClients = this.clients.filter(exitDate => !!exitDate);
}
Can one handle it this way? Will this filter function return objects that have a value for the property "exitDate" and exclude those that don't?
Upvotes: 2
Views: 206
Reputation:
You are almost right, but the filter function will give you as an argument each one of the objects and you'll need to look for the property and its value, so it would be something like this:
this.clients.filter(client => client.exitDate !== null);
The array's filter method walks the array and executes the callback for each of its items, so the first argument you get on the callback is the item itself.
In this case, you want to check for the exitDate property and return 'true' if it is not null.
Upvotes: 1