You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

请求完善Employee类:实现员工多参数排序与年龄过滤功能

求助:员工数据代码后续操作指导

我目前编写的代码,我完全不清楚接下来该如何操作,希望有人能提供帮助。

class Employee{
    
}
let employeeList = [
    {
       "id":1,
      "name": "Shivam",
      "salary":700000,
      "age":26
    },
    {
       "id":2,
      "name": "om",
      "salary":600000,
      "age":22
    },
    {
       "id":3,
      "name": "shubham",
      "salary":500000,
      "age":42
    },
    {
       "id":4,
      "name": "Ram",
      "salary":400000 ,
      "age":32
    }
  ]

常见后续操作示例

1. 完善Employee类

给类添加构造函数和实用方法,封装员工数据逻辑:

class Employee {
  constructor(id, name, salary, age) {
    this.id = id;
    this.name = name;
    this.salary = salary;
    this.age = age;
  }

  // 打印员工详情
  showDetails() {
    console.log(`员工ID: ${this.id}, 姓名: ${this.name}, 薪资: ${this.salary}, 年龄: ${this.age}`);
  }
}

2. 转换数组为Employee实例

把现有普通对象数组转成类实例,方便调用类方法:

const employeeInstances = employeeList.map(item => new Employee(item.id, item.name, item.salary, item.age));
// 调用示例
employeeInstances[0].showDetails();

3. 数据筛选与统计

  • 筛选薪资超过50万的员工:
const highSalaryStaff = employeeList.filter(emp => emp.salary > 500000);
  • 按年龄从小到大排序:
const sortedByAge = [...employeeList].sort((a, b) => a.age - b.age);
  • 计算全体员工平均薪资:
const avgSalary = employeeList.reduce((total, emp) => total + emp.salary, 0) / employeeList.length;

内容的提问来源于stack exchange,提问作者kim

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.06 04:16:07