Waiting Answer November 05, 2023

Loop Through Associative Array in JavaScript

Answers
2024-01-18 12:41:47

To loop through an associative array in JavaScript, you can use the for...in loop. Here’s an example:

const myArray = {
  "key1": "value1",
  "key2": "value2",
  "key3": "value3"
};

for (const key in myArray) {
  console.log(`Key: ${key}, Value: ${myArray[key]}`);
}

This will output:

Key: key1, Value: value1
Key: key2, Value: value2
Key: key3, Value: value3

 

In the above example, myArray is an associative array with three key-value pairs. The for...in loop iterates over each key in the array and logs the key-value pair to the console.

Your Answer