ben657
ben657

Reputation: 3853

TypeScript, Looping through a dictionary

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; } = {};

Upvotes: 351

Views: 581554

Answers (11)

marko424
marko424

Reputation: 5346

If you want to loop only through object value take a look at Object.values

Upvotes: 1

Yuchen
Yuchen

Reputation: 33036

With es2019, this is now even simpler:

  1. We can use of systematic
  2. 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

Upvotes: 3

the-witcher-knight
the-witcher-knight

Reputation: 610

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] }));
}

Upvotes: 0

k06a
k06a

Reputation: 18755

Shortest way to get all dictionary/object values:

Object.keys(dict).map(k => dict[k]);

Or this ways:

Object.entries(dict).map([k,v] => /* ... */);

Upvotes: 32

Stephen Paul
Stephen Paul

Reputation: 39015

< ES 2017:

Object.keys(obj).forEach(key => {
  let value = obj[key];
});

>= ES 2017:

Object.entries(obj).forEach(
  ([key, value]) => console.log(key, value)
);

Upvotes: 333

Binh Ho
Binh Ho

Reputation: 4934

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.

Upvotes: 3

finnk
finnk

Reputation: 133

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`
}

Upvotes: 1

Ribeiro
Ribeiro

Reputation: 374

To get the keys:

function GetDictionaryKeysAsArray(dict: {[key: string]: string;}): string[] {
  let result: string[] = [];
  Object.keys(dict).map((key) =>
    result.push(key),
  );
  return result;
}

Upvotes: 0

Jamie Starke
Jamie Starke

Reputation: 9234

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];
    }
}

Upvotes: 56

Ian
Ian

Reputation: 50905

To loop over the key/values, use a for in loop:

for (let key in myDictionary) {
    let value = myDictionary[key];
    // Use `key` and `value`
}

Upvotes: 462

Resigned June 2023
Resigned June 2023

Reputation: 4937

How about this?

for (let [key, value] of Object.entries(obj)) {
    ...
}

Upvotes: 125

Related Questions