Miguel Moura
Miguel Moura

Reputation: 39364

Get keys and values of object

I have the following object in typescript:

let parameters = { include: 'tags', limit: 20, published: true };

How can I loop through each parameter and get its key and value?

I need to do something with the key and value when value is not undefined.

Upvotes: 0

Views: 52

Answers (2)

T.J. Crowder
T.J. Crowder

Reputation: 1074078

Use Object.entries, perhaps with a for-of loop and destructuring:

for (const [key, value] of Object.entries(parameters)) {
    if (value !== undefined) {
        // ...use `key` and `value` here
    }
}

Upvotes: 4

Ashok
Ashok

Reputation: 753

Use Object.keys

 Object.keys(parameters).forEach(key => {

    });

Upvotes: 0

Related Questions