私はJavascriptでコンストラクタを作成する方法を学んでいますが、それでも良いですが、配列から特定の項目を削除するメソッドを作る方法を苦労しています。Javascriptで配列を削除するメソッドを作る方法
Manager.fireEmployee(nameOfEmployee)を呼び出して、その従業員を配列から削除するときはいつでも必要です。
また、コンストラクタから新しい従業員を作成し、配列内に自動的にプッシュされる方法がありますか?
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];
使用[Array.prototype.splice()](https://developer.mozilla.org/ Array.prototype.push()](https://開発者)から項目を削除するには、次のように入力します(en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/splice)。 mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/push)を使用して配列に項目を追加します。 [Array documentation](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array)が役立つことを願っています。 –
これはおそらくここで答えています:http://stackoverflow.com/a/5767357/2782135 –