typescript 打字稿循环遍历类类型属性

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/30944763/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 06:41:54  来源:igfitidea点击:

Typescript looping through class type properties

typescriptreflection

提问by meji

How can one loop through the properties of a class in TypeScript? Take the following class for example:

如何遍历 TypeScript 中类的属性?以下面的类为例:

export class Task implements Itask {
  public Id: number = 0;
  public Name: string;
  public Description: string;
  public Completed: boolean = false;
  public TaskType: TaskType;
}

Im want to retrieve the properties, hence: ["Id", Name", "Description", "Completed", "TaskType"]

我想检索属性,因此: ["Id", Name", "Description", "Completed", "TaskType"]

Tried

试过

GetTaskHeaders = () => {
  const _self = this;
  const tHead = $('<thead />').append('<tr />');

  for (let i = 0; typeof TodoApp.Task.arguments; i++) {
    const th = $('<th />');
    th.append(TodoApp.Task.arguments[i]);
    tHead.append(th);
  }

  console.log(tHead);

  return tHead;
};  

Unfortunately without success, i know using "TodoApp.Task.arguments" is incorrect. However, can someone show me the right way please?

不幸的是没有成功,我知道使用“TodoApp.Task.arguments”是不正确的。但是,有人可以告诉我正确的方法吗?

回答by loretoparisi

Let's consider that all "not defined" properties i.e. all properties that are defined in the typescript class like (I wrote "not defined" and not undefinedfor a reason that will be clear below)

让我们考虑一下所有“未定义”属性,即所有在 typescript 类中定义的属性(我写了“未定义”而不是undefined因为下面会清楚的原因)

class A { 

   prop1: string
   prop2: number

} 

will not be enumerated by any of Object.keysor this.hasOwnProperty(k)since the autogen javascript has no knowledge of those properties. You have only one option, when you create your typescript class, that is to initialize all properties to default values like

不会被任何Object.keysthis.hasOwnProperty(k)因为 autogen javascript 不知道这些属性的枚举。当您创建打字稿类时,您只有一种选择,即将所有属性初始化为默认值,例如

class A { 

   prop1: string
   prop2: number
   prop3: B

   constructor() {
     this.prop1="";
     this.prop2=-1;
     this.prop3=null;
   }

} 

At this point you will get all the properties of Ainstance like in this mapping iteration from a dictionary

此时,您将从A字典中获取实例的所有属性,例如在此映射迭代中

var a = new A();
for (var i in properties) {
   if (a.hasOwnProperty(i)) {
      a[i]=properties[i];
   }
}

If you don't like the default values solution, you can still do this using the magic undefinedjavascript keyword so that you do:

如果您不喜欢默认值解决方案,您仍然可以使用 magic undefinedjavascript 关键字来执行此操作,以便您执行以下操作:

class A { 

   prop1: string = undefined
   prop2: number = undefined

} 

At this point the javascript counterpart will have all properties in the model and you will iterate them with Object.keys(this)or inspect them via the this.hasOwnProperty

此时,javascript 对应物将拥有模型中的所有属性,您将使用Object.keys(this)它们进行迭代或检查它们 this.hasOwnProperty

回答by Roy J

See How do I loop through or enumerate a JavaScript object?

请参阅如何循环或枚举 JavaScript 对象?

In your case, something like:

在您的情况下,类似于:

for (var i in TodoApp.Task) {
    if (TodoApp.Task.hasOwnProperty(i)) {
        var th = $('<th />').append(TodoApp.Task[i]);
        tHead.append(th);
    }
}