javascript - Create new array from exisiting properties -
i have object such:
export var project: = { 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 app , want add search element in it, can search different teammembers. want initialize properties 'name' out of every teammember. that's thing needs filtered.
i've thought of 2 ways:
- create new array names of project > teammembers in it.
- just initialize names. haven't succeeded this.
can me make right choice , explain me how can succeed? don't how make new array out of existing one. thinking this:
var teammembers = project.teammembers.name();
but apart option, best if use name property out of object.
i 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 typescript file have following:
initializeteammembers() { this.project.teammembers; } getteammembers(ev) { // reset teammembers of teammembers this.initializeteammembers(); // set val value of searcbar let val = ev.target.value; // if value 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 doesn't work. error : cannot read property 'filter' of undefined.
is there please me? in advance.
you can create new different array using array.prototype.map:
var teammembers = project.teammembers.map(function(item){ return item.name });
var project = { id: "1", name: "hunkemoller", teammembers: [ { id: 1, name: "sarah johnson", firstname: "sarah", role: "manager" }, { id: 2, name: "christian johnson", firstname: "christian", role: "employee" } ] }; console.log(project.teammembers.map(function(item){ return item.name }));
the better way create pipe filter results using array.prototype.filter.
Comments
Post a Comment