-1

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];
  • Use [Array.prototype.splice()](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/splice) to delete item from the Array and [Array.prototype.push()](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/push) to add the item to array. I hope you will find [Array documentation](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array) useful. – Alexander Arutinyants Jan 22 '17 at 11:58
  • Probably this is answered here: http://stackoverflow.com/a/5767357/2782135 – Alexander Arutinyants Jan 22 '17 at 12:01

2 Answers2

1

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());
baao
  • 62,535
  • 14
  • 113
  • 168
0

If you are asking how to delete the class instance as well as from array here is the solution.

fireEmployee(nameOfEmployee){
    var arrayIdx;
    for(var i=0;i<arr.length;i++){
     if(arr[i].name == nameOfEmployee){
       arrayIdx = i;
      }
    }
    delete arr.splice(arrayIdx,1); // removes from the array as well as the class instance
    }

and to push you can use the push method arr.push(employeeClassInstance) in your function

acesmndr
  • 5,249
  • 2
  • 19
  • 24