How to make a method that removes tables in Javascript

advertisements

I am learning how to make constructors in Javascript, so far so good but I am struggling how to make an method that will delete specific items from the array.

I want whenever I call Manager.fireEmployee(nameOfEmployee) to delete that employee from the array.

Also is there a way whenever I create new employee from the constructor that will be pushed automatically inside the array?

Here is the code:

class Employee {
    constructor(name, department) {
        this.name = name;
        this.department = department;
    }
    whoAreYou() {
        return `My name is ${this.name} and I am working in ${this.department} department`;
    }
};

class Manager extends Employee {
    constructor(name) {
        super(name, 'General');
    }
    fireEmployee(nameOfEmployee){
        // how to make this method so when I type the name of the employee it will remove it from the array?
    }
};

class SalesPerson extends Employee {
    constructor(name, quota) {
        super(name, 'Sales', quota);
        this.quota = quota;
    }
};

let michael = new Manager('Michael');
let pam = new Employee('Pam', 'Marketing');
let jim = new SalesPerson('Jim', '1000');
let dwight = new SalesPerson('Dwight', '1200');

let arr = [pam, jim, dwight];


I'd go with a slightly different approach for you Manager class, making it hold the employees as an array. Then you can call all the methods to manipulate the employees array easily from your Manager instance, e.g.

michael.addEmployee(pam);
michael.fireEmployee(pam);


class Manager extends Employee {
    constructor(name) {
        super(name, 'General');
        this.employees = [];
    }

    addEmployee(employee) {
        if (employee) this.employees.push(employee);
    }

    fireEmployee(employee){
        this.employees.splice(this.employees.indexOf(employee), 1);
    }

    getEmployees() {
        return this.employees;
    }
}

Now try this:

class Employee {
  constructor(name, department) {
    this.name = name;
    this.department = department;
  }
  whoAreYou() {
    return `My name is ${this.name} and I am working in ${this.department} department`;
  }
}

class Manager extends Employee {
  constructor(name) {
    super(name, 'General');
    this.employees = [];
  }

  addEmployee(employee) {
    if (employee) this.employees.push(employee);
  }

  fireEmployee(employee) {
    this.employees.splice(this.employees.indexOf(employee), 1);
  }

  getEmployees() {
    return this.employees;
  }
}

class SalesPerson extends Employee {
  constructor(name, quota) {
    super(name, 'Sales', quota);
    this.quota = quota;
  }
}

let michael = new Manager('Michael');
let pam = new Employee('Pam', 'Marketing');
let jim = new SalesPerson('Jim', '1000');
let dwight = new SalesPerson('Dwight', '1200');

michael.addEmployee(pam);
michael.addEmployee(jim);
michael.addEmployee(dwight);

console.log(michael.getEmployees());

michael.fireEmployee(pam);

console.log(michael.getEmployees());