typescript for-in 语句

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12950681/
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-11 00:54:17  来源:igfitidea点击:

for-in statement

typescript

提问by Ido Ran

TypeScript docs say nothing about loop like foror for-in. From playing with the language it seems that only anyor stringvariables are supported in forloop.

TypeScript 文档没有提到像for或这样的循环for-in。从使用语言看来,循环中似乎只支持anystring变量for

Why has this decision been made?

为什么做出这个决定?

Why not use the type information and have strongly-typed iteration variable?

为什么不使用类型信息并具有强类型迭代变量?

采纳答案by Fenton

The for-in statement is really there to enumerate over object properties, which is how it is implemented in TypeScript. There are some issues with using it on arrays.

for-in 语句确实用于枚举对象属性,这就是它在 TypeScript 中的实现方式。在数组上使用它存在一些问题

I can't speak on behalf of the TypeScript team, but I believe this is the reason for the implementation in the language.

我不能代表 TypeScript 团队发言,但我相信这是在语言中实现的原因。

回答by brianbruff

In Typescript 1.5 and later, you can use for..ofas opposed to for..in

在 Typescript 1.5 及更高版本中,您可以使用for..of而不是for..in

var numbers = [1, 2, 3];

for (var number of numbers) {
    console.log(number);
}

回答by Ryan Cavanaugh

TypeScript isn't giving you a gun to shoot yourself in the foot with.

TypeScript 并没有给你一把枪来用脚射击自己。

The iterator variable is a string because it is a string, full stop. Observe:

迭代器变量是一个字符串,因为它是一个字符串,句号。观察:

var obj = {};
obj['0'] = 'quote zero quote';
obj[0.0] = 'zero point zero';
obj['[object Object]'] = 'literal string "[object Object]"';
obj[<any>obj] = 'this obj'
obj[<any>undefined] = 'undefined';
obj[<any>"undefined"] = 'the literal string "undefined"';

for(var key in obj) {
    console.log('Type: ' + typeof key);
    console.log(key + ' => ' + obj[key]);
}

How many key/value pairs are in objnow? 6, more or less? No, 3, and all of the keys are strings:

现在有多少键/值对obj?6、多还是少?不,3,所有的键都是字符串:

Type: string
0 => zero point zero
Type: string
[object Object] => this obj; 
Type: string
undefined => the literal string "undefined" 

回答by citykid

edit 2018: This is outdated, js and typescript now have for..of loops.
http://www.typescriptlang.org/docs/handbook/iterators-and-generators.html

2018 年编辑:这已经过时了,js 和 typescript 现在有 for..of 循环。
http://www.typescriptlang.org/docs/handbook/iterators-and-generators.html



The book "TypeScript Revealed" says

“TypeScript Revealed”一书说

"You can iterate through the items in an array by using either for or for..in loops as demonstrated here:

“您可以使用 for 或 for..in 循环遍历数组中的项目,如下所示:

// standard for loop
for (var i = 0; i < actors.length; i++)
{
  console.log(actors[i]);
}

// for..in loop
for (var actor in actors)
{
  console.log(actor);
}

"

Turns out, the second loop does notpass the actors in the loop. So would say this is plain wrong. Sadly it is as above, loops are untouched by typescript.

事实证明,第二个循环没有传递循环中的演员。所以会说这是完全错误的。可悲的是,如上所述,打字稿未触及循环。

mapand forEachoften help me and are due to typescripts enhancements on function definitions more approachable, lke at the very moment:

mapforEach经常帮助我,并且由于打字稿对函数定义的增强更加平易近人,就像此刻:

this.notes = arr.map(state => new Note(state));

My wish list to TypeScript;

我对 TypeScript 的愿望清单;

  1. Generic collections
  2. Iterators (IEnumerable, IEnumerator interfaces would be best)
  1. 通用集合
  2. 迭代器(IEnumerable、IEnumerator 接口最好)