loop through key value pairs js 1

loop through key value pairs js

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

Here is what the above code is Doing:
1. We create an object called object1.
2. We use the Object.entries() method to get an array of the object’s own enumerable string-keyed property [key, value] pairs.
3. We use a for…of loop to iterate over the array.
4. We log the key and value of each property to the console.

Similar Posts