2016-10-23 7 views
4

次のクラスが与えられた場合、そのプロパティを列挙するにはどうすればいいですか?つまり、[station1, station2, station3 ...]のような出力を得ることができます。プロパティの値を列挙する方法、つまり[null, null, null]しか見ることができません。TypeScriptオブジェクトのプロパティを列挙します

class stationGuide { 
    station1: any; 
    station2: any; 
    station3: any; 

    constructor(){ 
     this.station1 = null; 
     this.station2 = null; 
     this.station3 = null; 
    } 
} 
+0

あなたがObject.keys' 'で見たことがありますか? – evolutionxbox

答えて

10

あなたがObject.keys()、その後forEach、またはfor/inを使用するを使用して、2つのオプションが、持っている:

class stationGuide { 
    station1: any; 
    station2: any; 
    station3: any; 

    constructor(){ 
     this.station1 = null; 
     this.station2 = null; 
     this.station3 = null; 
    } 
} 

let a = new stationGuide(); 
Object.keys(a).forEach(key => console.log(key)); 

for (let key in a) { 
    console.log(key); 
} 

code in playground

関連する問題