• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar
The Electric Toolbox Blog

The Electric Toolbox Blog

Linux, Apache, Nginx, MySQL, Javascript and PHP articles

  • Applications
  • FCKEditor
  • Apache
  • Windows
  • Contact Us
Home / Loop through key value pairs from an associative array with Javascript

Loop through key value pairs from an associative array with Javascript

This post looks at how to loop through an associate array with Javascript and display the key value pairs from the array. An associative array can contain string based keys instead of zero or one-based numeric keys in a regular array.

If we had the following array defined in Javascript:

var items = {
  "foo" : 123456,
  "bar" : 789012,
  "baz" : 345678,
  "bat" : 901234
};

we could loop through the array and display the index and then the value like so:

for(var index in items) {
  document.write( index + " : " + items[index] + "<br />");
}

This would display the following:

foo : 123456
bar : 789012
baz : 345678
bat : 901234

Check Out These Related posts:

  1. Get unique array values with PHP
  2. Bash For Loop
  3. Type casting with PHP
  4. Iterate through an associative array the jQuery way

Filed Under: Javascript

Primary Sidebar

Categories

  • Apache
  • Applications
  • Article
  • Case Studies
  • Email Servers
  • FCKEditor
  • HTML And CSS
  • Javascript
  • Linux/Unix/BSD
  • Microsoft SQL Server
  • Miscellaneous Postings
  • MySql
  • Networking
  • Nginx Web Server
  • Offsite Articles
  • OSX
  • PHP
  • Quick Tips
  • RFC – Request for Comments
  • SilverStripe
  • VMWare
  • VPN
  • Windows
  • WordPress

Recent Posts

  • Vim Show Line Numbers
  • Add User To Group Linux
  • Chmod 777 Tutorial
  • How to Copy Directory Linux
  • Linux create user

Copyright © 2021. ElectricToolBox. All Rights Reserved.

  • Contact Us
  • Copyright Info
  • Privacy Policy
  • Sitemap