Ich habe eine Komponente und einen Service:Angular 2 Typoskript wie Element zu finden in Array
Komponente:
export class WebUserProfileViewComponent {
persons: Person [];
personId: number;
constructor(params: RouteParams, private personService: PersonService) {
this.personId = params.get('id');
this.persons = this. personService.getPersons();
console.log(this.personId);
}
}
Service:
@Injectable()
export class PersonService {
getPersons(){
var persons: Person[] = [
{id: 1, firstName:'Hans', lastName:'Mustermann', email: '[email protected]', company:'Test', country:'DE'},
{id: 2, firstName:'Muster', lastName:'Mustermann', email: '[email protected]', company:'test', country:'DE'},
{id:3, firstName:'Thomas', lastName:'Mustermann', email: '[email protected]', company:'test', country:'DE'}
];
return persons;
}
}
Ich möchte die Person mit der ID ('personID') bekommen. Die personID erhalte ich von Routeparam. Dafür brauche ich die foreach-Schleife? Dafür habe ich keine Lösung gefunden.
Vielen Dank im Voraus!
Sie können ein Element von Id wie diese persons.find (Person => person.id === PersonId) – tstellfe