I have a Component and a Service:
Component:
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;
}
}
I want to get the Person Item with the Id ('personID'). The personID i get from Routeparam. For that I need the foreach loop? But I haven´t found a solution for this.
You need to use method Array.filter
:
this.persons = this.personService.getPersons().filter(x => x.id == this.personId)[0];
or Array.find
this.persons = this.personService.getPersons().find(x => x.id == this.personId);