Explore Topics

Loop Through Associative Array in JavaScript

Last Updated : 18 Apr, 2025 - Asked By Ashok

js  loops. associative array 

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



Other Resources

Quiz Image
Quiz

Test your knowledge with interactive quizzes.

Interview Questions Image
Interview Questions

Prepare for interviews with curated question sets.

Q&A Image
Q&A

Ask your coding-related doubts and get answers.

Certification Image
Certification

Earn certifications to enhance your resume.

internships Image
Internships

Hands-on projects to improve your skills.

Quiz Image
Quiz

Test your knowledge with interactive quizzes.

Interview Questions Image
Interview Questions

Prepare for interviews with curated question sets.

blog Image
Blogs

Add your technical blogs and read technical topics.

Certification Image
Certification

Earn certifications to enhance your resume.

Q&A Image
Q&A

Hands-on projects to improve your skills.

People Also Asked