JavaScript 浏览类的对象数组以打印每个属性



因此,我有一个Worker类,它扩展了Person类,以便为其提供您在下面看到的所有属性。

class Worker extends Person {
constructor(name, age, address, wage, position) {
super(name, age, address);
this.wage = 0;
this.position = "";
}
}

我还有一个EmployeeList类,它基本上是所有员工的数组:

class EmployeeList {
//Initialize studentList array
constructor(empList) {
this.empList = [];
}
//Add studentObject to studentList
addEmployee(employeeObject) {
this.empList.push(employeeObject);
}
//delete studentObject from studentList
deleteEmployee(employeeName) {
let name = this.empList.findIndex((employeeName) => {
return employeeName
});
this.empList.splice(name, 1);
}
}

因此,我遇到的问题是,我能够创建EmployeeList 的实例

let myEmployeeList = new EmployeeList();

并在其中填充员工。但是,我想遍历myEmployeeList中每个Employee的所有属性。我尝试过混合使用Object.keys、Object.Values和Object.entries,但都没有成功。我希望在控制台日志时输出如下:

name: George
age: 26
address: 18809 Oakridge Ct.
wage: 20
position: cook
name: Phil
age: 35
address: 4556 Royal Park Ave.
wage: 28
position: chef
name: Lizzy
age: 24
address: 1136 Rasberry Ct.
wage: 22
position: cook

最终,我希望能够按姓名、年龄或任何房产进行搜索,这就是为什么我试图找出如何遍历所有内容的原因。

我看到过类似的帖子,但没有发现任何能帮助我解决特定问题的帖子。如果一个已经存在,那么我为这个错误道歉

你的帖子中有两件事:

  1. 显示您的Worker

为什么不向类WorkerPerson添加显示方法。并通过迭代来迭代和调用employeeList中员工的显示方法。

  1. 使用EmployeeList查找房产价值

只需在EmployeeList中添加一个方法,即可查找您的任何员工属性是否包含您的搜索。像这样的东西:

searchEmployee(search) {
return empList.filter(employee => {
return Object.values(employee).includes(search);
})
}

您将获得一个新的员工列表,其中包含包含您搜索的属性。

最新更新