Looping through a 2-dimensional array with jQuery

April 21, 2009 by AcidRaZor · Leave a Comment
Filed under: JavaScript, Programming 101, jQuery 

I recently opened my eyes to the wonderful world of jQuery. *drifts off into a fantasy*

Here’s an example of how to loop through a 2-dimensional array using jQuery. Fun & Easy:

1
2
3
4
5
6
7
var currencyTocountryName = [['USD','U.S. Dollar (USD)','$'],['GBP','British Pound (GBP)','£'],['EUR','Euro (EUR)','€'],['AUD','Australian Dollar (AUD)','$'],['CAD','Canadian Dollar (CAD)','$'],['NZD','New Zealand Dollar (NZD)','$']]
$.each( currencyTocountryName,
           function( i, value ){
                   alert(value[0]);
                   alert(value[1]);
            }
);

As you can see it’s pretty straight forward. No need to have “i” be the place holder, a simple value[0] will get you the first value in the array. This must be the easiest implementation of a loop and array I’ve done in years. Sure, by now I can do a simple JavaScript loop in my sleep. But without thinking and a lot less code, jQuery allows me to leverage off that power. *Drifts off into a fantasy once again*

SEO Powered by Platinum SEO from Techblissonline