Categories
JavaScript Answers

How to print an object’s keys and values with JavaScript?

Spread the love

Sometimes, we want to print an object’s keys and values with JavaScript.

In this article, we’ll look at how to print an object’s keys and values with JavaScript.

How to print an object’s keys and values with JavaScript?

To print an object’s keys and values with JavaScript, we can use Object.entries and the for-of loop.

For instance, we write:

const filters = {
  "user": "abc",
  "application": "xyz"
}

for (const [key, val] of Object.entries(filters)) {
  console.log(key, val)
}

We loop through the key-value pairs of filters by convert it to an array of key-value pair arrays with Object.entries.

In the for-of loop, we destructure the key and value from each array entry into key and val and log them.

Therefore, we see:

user abc
application xyz

logged.

Conclusion

To print an object’s keys and values with JavaScript, we can use Object.entries and the for-of loop.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *