「TypeScript」for文でオブジェクトのプロパティを出力する

環境
Windows10 64bit
TypeScript 4.4.4

書式
type クラス名 = {
メンバー変数名1:データの型;
メンバー変数名2:データの型;
};
const オブジェクト名:クラス名 = {
変数名1:値1,
変数名2:値2
}
使用例

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
//クラスStudentの定義
type Student = {
//プロパティの定義
username: string;
age: number;
addr: string;
};
//オブジェクトの初期化
const stuObj: Student = {
username: '山田二郎',
age: 32,
addr:"東京新宿区"
};
console.log("全てのプロパティを出力する結果")
//for文でプロパティを全て処理る
for (const prop in stuObj) {
console.log(prop);
}
//クラスStudentの定義 type Student = { //プロパティの定義 username: string; age: number; addr: string; }; //オブジェクトの初期化 const stuObj: Student = { username: '山田二郎', age: 32, addr:"東京新宿区" }; console.log("全てのプロパティを出力する結果") //for文でプロパティを全て処理る for (const prop in stuObj) { console.log(prop); }
//クラスStudentの定義
type Student = {
  //プロパティの定義
  username: string;
  age: number;
  addr: string; 
};
//オブジェクトの初期化
const stuObj: Student = {
  username: '山田二郎',
  age: 32,
  addr:"東京新宿区"
};
console.log("全てのプロパティを出力する結果")
//for文でプロパティを全て処理る
for (const prop in stuObj) {
  console.log(prop);
}

実行結果
C:\typescript>tsc
C:\typescript>ts-node arkgame.js
全てのプロパティを出力する結果
username
age
addr

TypeScript

Posted by arkgame