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

Recently Asked Questions

PHP is a server-side scripting language designed for web development but also used as a general-purpose programming language.PHP is a server-side scripting language designed for web development but also used as a general-purpose programming language.

Python has a variety of libraries such as NumPy, pandas, and matplotlib that make it an ideal language for data analysis and visualization.

Java is commonly used for building enterprise-scale applications.