如何在Typescript中的列表中查找对象的属性

时间:2019-01-27 20:32:24

标签: javascript angular typescript

我有一个雇主列表,我需要根据其名称从列表中检索一个雇主的ID。后续功能是从另一个API获取雇主列表。

getEmployers(): void {
    this.employersService.getEmployers().subscribe((employerData: Employer[])=>{this.employerList = employerData})
  }

我需要从该ownerList中找到名称为“ John”的雇主的ownerId。

我该如何实现?

2 个答案:

答案 0 :(得分:0)

尝试类似

const employer = this.employerList.find(x => x.name === "John");
return employer && employer.id;

希望有帮助。

答案 1 :(得分:0)

getEmployers():无效{     this.employersService.getEmployers()。find(employee => employee.name ==='John')。subscribe((employee)=> { / **这是雇员约翰** / this.employee =雇员; })   }