Top community members
All Questions Ask Question

Have you ever heard about encyclopedia for code?

What about: Wiki for Code project?

* because I've tried to share code and my solutions on different services and it was removed or I was unwelcomed
   - maybe it wasn't good place, but this one is.

if you think you have solution that can help someone, you are welcome to share this solution - Click here

TypeScript - iterate over dictionary

1 answers
3 points

How can I iterate over dictionary of objects in TypeScript?
I have map of object like this:

let dictionary = {
    'key1' : {
        id : 47,
        name : 'Test 1'
    },
    'key2' : {
        id : 51,      
        name : 'Test 2'
    },
    'key3' : {
        id : 141,
        name : 'Test 3'
    },
};

What is the best and recommended way of doing that?

1 answers
2 points

Quick solution:

for(let key in dictionary) {
    let value = dictionary[key];

    let id = value.id;
    let name = value.name;
}

We can use any other property defined under dictionary value, by using value.propertyName.

We can use standrad for...in loop over object from JavaScript, code example:

// ONLINE-RUNNER:browser;

let dictionary = {
    'key1' : {
        id : 47,
        name : 'Test 1'
    },
    'key2' : {
        id : 51,      
        name : 'Test 2'
    },
    'key3' : {
        id : 141,
        name : 'Test 3'
    },
};

for(let key in dictionary) {
    let value = dictionary[key];

    let id = value.id;
    let name = value.name;

    console.log('key: ' + key + ', id: ' + id  + ', name: ' + name);
}

You can take a look at article, point 3 (3. for...in loop over object example)

 

0 comments Add comment

Checkout latest Findings & News:

Checkout latest questions:

Checkout latest wiki articles:

Hey 👋
Would you like to know what we do?
  • Dirask is IT community, where we share coding knowledge and help each other to solve coding problems.
  • We welcome everyone,
    no matter what the experience,
    no matter how basic the question is,
    this community will help you.
Read more