admin管理员组文章数量:1435859
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; } = {};
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; } = {};
Share
Improve this question
edited Apr 28, 2022 at 9:14
Yves M.
31k24 gold badges109 silver badges149 bronze badges
asked Apr 23, 2013 at 16:07
ben657ben657
3,8533 gold badges18 silver badges15 bronze badges
3
|
11 Answers
Reset to default 462To loop over the key/values, use a for in
loop:
for (let key in myDictionary) {
let value = myDictionary[key];
// Use `key` and `value`
}
< ES 2017:
Object.keys(obj).forEach(key => {
let value = obj[key];
});
>= ES 2017:
Object.entries(obj).forEach(
([key, value]) => console.log(key, value)
);
How about this?
for (let [key, value] of Object.entries(obj)) {
...
}
There is one caveat to the key/value loop that Ian mentioned. If it is possible that the Objects may have attributes attached to their Prototype, and when you use the in
operator, these attributes will be included. So you will want to make sure that the key is an attribute of your instance, and not of the prototype. Older IEs are known for having indexof(v)
show up as a key.
for (const key in myDictionary) {
if (myDictionary.hasOwnProperty(key)) {
let value = myDictionary[key];
}
}
Shortest way to get all dictionary/object values:
Object.keys(dict).map(k => dict[k]);
Or this ways:
Object.entries(dict).map([k,v] => /* ... */);
If you just for in
a object without if statement hasOwnProperty
then you will get error from linter like:
for (const key in myobj) {
console.log(key);
}
WARNING in component.ts
for (... in ...) statements must be filtered with an if statement
So the solutions is use Object.keys
and of
instead.
for (const key of Object.keys(myobj)) {
console.log(key);
}
Hope this helper some one using a linter.
With es2019, this is now even simpler:
- We can use
of
systematic - No longer need to wrap the dictionary with
Object.entries
Example:
let someMap: Map<number, number> = new Map()
someMap.set(3, 7);
someMap.set(4, 12);
for (let [key, value] of someMap) {
console.log(key, value)
}
Output:
3 7
4 12
Ians Answer is good, but you should use const instead of let for the key because it never gets updated.
for (const key in myDictionary) {
let value = myDictionary[key];
// Use `key` and `value`
}
If you want to loop only through object value take a look at Object.values
To get the keys:
function GetDictionaryKeysAsArray(dict: {[key: string]: string;}): string[] {
let result: string[] = [];
Object.keys(dict).map((key) =>
result.push(key),
);
return result;
}
this is my function, i hope this help
function recordToArray<TypeOfSchema>(
data: Record<string, TypeOfSchema>
): Array<TypeOfSchema> {
return Object.keys(data).map((key: string) => ({ id: key, ...data[key] }));
}
本文标签: javascriptTypeScriptLooping through a dictionaryStack Overflow
版权声明:本文标题:javascript - TypeScript, Looping through a dictionary - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736698273a1948297.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
for (var key in myDictionary) { }
? Inside the loop, you'd usekey
to get the key, andmyDictionary[key]
to get the value – Ian Commented Apr 23, 2013 at 16:15