Angular 2 TypeScript how to find element in Array

前端 未结 7 798
抹茶落季
抹茶落季 2021-01-29 21:35

I have a Component and a Service:

Component:

7条回答
  •  猫巷女王i
    2021-01-29 21:41

    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);
    

提交回复
热议问题