循环遍历枚举、TypeScript 和 JQuery
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/30653819/
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
Looping through an enum, TypeScript and JQuery
提问by meji
Hello im trying t develop a straight forward todo app using TypeScript and JQuery. I have an enum that lists task types:
你好,我正在尝试使用 TypeScript 和 JQuery 开发一个直接的待办事项应用程序。我有一个列出任务类型的枚举:
export enum TaskType { FrontEnd, BackEnd, Designer };
However looping through the emum using jquery.each or for loop, i get the following result, (values then indexes):
但是使用 jquery.each 或 for 循环遍历 emum,我得到以下结果,(值然后索引):
FrontEnd, BackEnd, Designer, 0, 1, 2
The following is the code i loop through the enum:
以下是我遍历枚举的代码:
constructor(e?: Object) {
var template = this.FormTemplate;
$(e).append(template);
var sel = template.find('select');
/*$.each(TaskType, function (index, el) {
sel.append("<option value='" + index + "'>" + el + "</option>");
});*/
for(var i=0; i < (typeof TaskType).length; i++){
sel.append("<option value='" + TaskType[i] + "'>" + TaskType[i] + "</option>");
}
}
Can someone tell me why this is?
有人能告诉我这是为什么吗?
回答by jfriend00
TypeScript enums when compiled into plain JS contain both the symbolic name AND the numeric values as properties and that explains why you get FrontEnd, BackEnd, Designer, 0, 1, 2
when you try to enumerate the properties of the object. As best I know, there is no post-compile way to enumerate only the symbolic names. You could enumerate all of them and skip anything that is a number.
编译成普通 JS 时的 TypeScript 枚举包含符号名称和数字值作为属性,这解释了为什么FrontEnd, BackEnd, Designer, 0, 1, 2
当您尝试枚举对象的属性时会得到。据我所知,没有编译后的方法来仅枚举符号名称。您可以枚举所有这些并跳过任何数字。
From this article, you can see exactly how a TypeScript enum compiles into JS.
从本文中,您可以确切地看到 TypeScript 枚举是如何编译成 JS 的。
If you have this TypeScript:
如果你有这个 TypeScript:
//TypeScript declaration:
enum StandardEnum {FirstItem, SecondItem, ThirdItem};
It compiles to this Javscript:
它编译成这个 Javscript:
//Compiled javascript:
var StandardEnum;
(function (StandardEnum) {
StandardEnum[StandardEnum["FirstItem"] = 0] = "FirstItem";
StandardEnum[StandardEnum["SecondItem"] = 1] = "SecondItem";
StandardEnum[StandardEnum["ThirdItem"] = 2] = "ThirdItem";
})(StandardEnum || (StandardEnum = {}));
;
Which is essentially this result:
这基本上是这个结果:
var StandardEnum = {
"FirstItem": 0,
"SecondItem": 1,
"ThirdItem": 2,
"0": "FirstItem",
"1": "SecondItem",
"2": "ThirdItem"
};
So, unless you specifically ignore the numeric properties, there is no way to enumerate just the enum names.
因此,除非您专门忽略数字属性,否则无法仅枚举枚举名称。
You could do that like this:
你可以这样做:
for (var item in StandardEnum) {
if (StandardEnum.hasOwnProperty(item) && !/^\d+$/.test(item)) {
console.log(item);
}
}
Working demo: http://jsfiddle.net/jfriend00/65cfg88u/
工作演示:http: //jsfiddle.net/jfriend00/65cfg88u/
FYI, if what you really want is:
仅供参考,如果您真正想要的是:
var StandardEnum = {
"FirstItem": 0,
"SecondItem": 1,
"ThirdItem": 2
};
Then, maybe you should not use the enum and just use this standard JS declaration. Then, you could get the properties with Object.keys(StandardEnum)
.
那么,也许你不应该使用枚举而只使用这个标准的 JS 声明。然后,您可以使用Object.keys(StandardEnum)
.