Skip to content
the poor coder

iterate key value object javascript

Poor Man
Published
Reading time
1 min read

Iterating through a key-value object in JavaScript is relatively straightforward. It can be done using either a for-in loop or the Object.entries() method. Both of these approaches provide an easy way to loop through an object's key-value pairs and access the corresponding values.

Text

// Using a for-in loop
for (const key in obj) {
    const value = obj[key];
    console.log(`${key}: ${value}`);
}

// Using Object.entries()
const entries = Object.entries(obj);
for (const [key, value] of entries) {
    console.log(`${key}: ${value}`);
}

In the case of a for-in loop, the key-value pairs of the object can be accessed by looping over the object's keys and accessing the value associated with each particular key. With Object.entries(), the key-value pairs of the object are returned as an array of two-element arrays, which can then be looped over to access the key-value pairs. Both of these approaches are useful for looping through an object's key-value pairs and printing out the corresponding values.

Keep going

2023

how to sort an array

How to Sort an Array If you have an array and you want to order its elements in a specific way, you need to use a sorting algorithm. There are several sorting algorithms available, but two of the most commonly used are…

javascript2 min read
2023

jquery mobile or desktop

jQuery Mobile or Desktop? As a web developer, I have had some experience with both jQuery Mobile and jQuery Desktop. Both frameworks have their pros and cons, and which one to use really depends on the specific project…

javascript1 min read
2023

node.js folder structure

Node.js Folder Structure If you are new to Node.js, it can be daunting to create a folder structure that will work for your project. The structure you choose will depend on the size and complexity of your project. Here…

javascript1 min read
ESC