2017-03-07 37 views
-1

我想在Angular 2類中實現一個過濾器,但由於某種原因我出現了一個錯誤。我無法過濾並返回結果。Angular 2 - 過濾數組對象不工作

類代碼:

employee = [ 
    { "empId": "59C", "empDesc": "Software","location":"Dallas"}, 
    { "empId": "AMI", "empDesc": "Hardware", "location":"San Francisco"} 
    ]; 
    companies : any = []; 
    comapny : any = []; 
    test:string="varun"; 
    companyId = this.employee[0].empId; 
    constructor(){ 
    for (let c of this.employee) { 
     this.companies.push({ 
     empDesc: c.empDesc, 
     empId: c.empId 
     }) 
    }; 
    console.log("Companies",this.companies); 
    } 
    getCompanies(companies){ 
    if (this.companyId) { 
        var filtered = companies.filter(function (company) { 
         return company.companyId === this.companyId; 
        }); 

        return filtered[0]; 
       } 

    } 

    this.company = this.getCompanies(this.companies); 
    console.log("Filtered company", this.company); 

Plunker鏈接

https://plnkr.co/edit/CnBR4JouNhzH3DWm7QCo?p=preview

回答

3

總是在構造函數中運行代碼,也是公司沒有companyId,這會工作:

import { Component } from '@angular/core'; 

@Component({ 
    selector: 'my-app', 
    template: ` 
    <h1>Hello {{name}}</h1> 
    <ul><li *ngFor="let c of companies">{{c.empId}} - {{c.empDesc}}</li></ul> 
    ` 
}) 
export class AppComponent { 
    employee = [ 
    { "empId": "59C", "empDesc": "Software","location":"Dallas"}, 
    { "empId": "AMI", "empDesc": "Hardware", "location":"San Francisco"} 
    ]; 
    companies : any = []; 
    comapny : any = []; 
    test:string="varun"; 
    companyId = this.employee[0].empId; 
    constructor(){ 
    for (let c of this.employee) { 
     this.companies.push({ 
     empDesc: c.empDesc, 
     empId: c.empId 
     }) 
    }; 
    console.log("Companies",this.companies); 
    this.company = this.getCompanies(this.companies); 
    console.log("Filtered company", this.company); 
    } 
    getCompanies(companies){ 
    let self = this; 
    if (this.companyId) { 
        var filtered = companies.filter(function (company) { 
         return company.empId === self.companyId; 
        }); 

        return filtered[0]; 
       } 

    } 


} 
+0

謝謝,這工作。但是,爲什麼我們使用,讓自己=這個;在函數中?當我用「return company.empId === self.companyId;」替換自己時,該函數不起作用。你能告訴我爲什麼會這樣嗎? – Varun

+1

,因爲如果我們使用'this',它會引用沒有'comanyId'字段的'companies'數組。換句話說,這個類的「this」將會被它的過濾方法中的'company'數組的'this'遮蔽。 –

3

您plunker不起作用。您的問題與最後兩行有關:

this.company = this.getCompanies(this.companies); 

console.log("Filtered company", this.company); 

您不應該直接在類中調用方法或console.log。你需要把這個東西放在你的構造函數中。像:

employee = [ 
    { "empId": "59C", "empDesc": "Software","location":"Dallas"}, 
    { "empId": "AMI", "empDesc": "Hardware", "location":"San Francisco"} 
]; 
companies : any = []; 
comapny : any = []; 
test:string="varun"; 
companyId = this.employee[0].empId; 
constructor(){ 
    for (let c of this.employee) { 
    this.companies.push({ 
     empDesc: c.empDesc, 
     empId: c.empId 
    }) 
    }; 
    this.company = this.getCompanies(this.companies); 
    console.log("Filtered company", this.company); 
    console.log("Companies",this.companies); 
} 
getCompanies(companies){ 
    if (this.companyId) { 
       var filtered = companies.filter(function (company) { 
        return company.empId === this.companyId; 
       }); 

       return filtered[0]; 
      } 

} 
+1

而在'companies'一個'company'沒有一個叫做'場companyId'。(只有'empId')也許他應該在他的情況下使用'Array.find'而不是'Array.filter'。 – cyrix

+0

並請請..使用箭頭函數作爲回調..這將防止在你的代碼中的很多'這'混淆.. – MikeOne