Skip to main content

Loop Through Associative Array in JavaScript

Published: Asked By 1 Answers Answered

How to Loop Through a JavaScript Associative Array Object

Share:

1 Answer

S
Sooraj Community Contributor Answered on

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
Certifications

Earn certifications to enhance your resume.