2017-01-22 11 views
-1

私は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]; 
+0

使用[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)が役立つことを願っています。 –

+0

これはおそらくここで答えています:http://stackoverflow.com/a/5767357/2782135 –

答えて

1

をプッシュ方式arr.push(employeeClassInstance)を使用することができますプッシュします。次に、すべてのメソッドを呼び出して、Managerインスタンスから簡単にemployees配列を操作することができます。

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; 
    } 
} 

さて、これを試してみてください。

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());

+1

すごくすごい、仲間に感謝します。 –

0

あなたがここ配列からだけでなく、クラスのインスタンスを削除する方法を求めている場合は、ソリューションです:ここでは

はコードです。

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 
    } 

、あなたは、私はそれが配列として従業員を保持すること、あなたManagerクラスのため、わずかに異なるアプローチで行くと思いますあなたの関数に

+0

ありがとうございます:)。 –

関連する問題