TypeScript,通过字典循环 [英] TypeScript, Looping through a dictionary
本文介绍了TypeScript,通过字典循环的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在我的代码中,我有几个字典(正如建议这里),它是String索引的。由于这是一种即兴类型,我想知道是否有任何关于如何能够遍历每个键(或值,我需要键的所有内容)的建议。任何帮助赞赏!
myDictionary:{[index:string]:any; } = {};
解决方案
要循环使用键/值, code $ for循环:
$ p $ for(let key in myDictionary){
let value = myDictionary [key];
//使用`key`和`value`
}
In my code, I have a couple of dictionaries (as suggested here) which is String indexed. Due to this being a bit of an improvised type, I was wondering if there any suggestions on how I would be able to loop through each key (or value, all I need the keys for anyway). Any help appreciated!
myDictionary: { [index: string]: any; } = {};
解决方案
To loop over the key/values, use a for in
loop:
for (let key in myDictionary) {
let value = myDictionary[key];
// Use `key` and `value`
}
这篇关于TypeScript,通过字典循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文