I have an object as such:
export var PROJECT: any = {
id: "1",
name: "Hunkemoller",
teammembers: [
{
id: 1,
name: "Sarah Johnson",
firstname: "Sarah",
role: "Manager"
},
{
id: 2,
name: "Christian Johnson",
firstname: "Christian",
role: "Employee"
}
etc.
I'm making an app and I want to add a search element in it, so you can search for the different teammembers. I want to initialize all the properties 'name' out of every teammember. That's the only thing that needs to be filtered.
I've thought of 2 ways:
- Create a new Array with only the names of the project > teammembers in it.
- Just initialize the names. But I haven't succeeded with this.
Can you help me make the right choice and explain to me how I can succeed? I don't even now how to make a new Array out of the existing one. I was thinking something like this:
var teamMembers = project.teammembers.name();
But apart from that option, it would be the best if I could just use the name property out of the object.
I was this far (I'm using Ionic 2 / Angular 2)
<ion-searchbar (ionInput)="getTeammembers($event)" [(ngModel)]="searchQuery"></ion-searchbar>
<ion-list>
<ion-item *ngFor="let teammember of project.teammembers">
{{ teammember.name }}
</ion-item>
</ion-list>
In my typescript file I have the following:
initializeTeammembers() {
this.project.teammembers;
}
getTeammembers(ev) {
// reset teammembers back to all of the teammembers
this.initializeTeammembers();
// set val to the value of the searcbar
let val = ev.target.value;
// if the value is empty string, don't filter teammembers
if (val && val.trim() != '') {
this.project.teammembers.name = this.project.teammembers.name.filter((teammember) => {
return (teammember.toLowerCase().indexOf(val.toLowerCase()) > -1);
})
}
}
But it doesn't work. I get the error : Cannot read property 'filter' of undefined.
Is there someone who please could help me? Thanks in advance.