CodingForums.com

CodingForums.com (http://www.codingforums.com/index.php)
-   JavaScript programming (http://www.codingforums.com/forumdisplay.php?f=2)
-   -   What are some other ways to write JavaScript arrays? (http://www.codingforums.com/showthread.php?t=263185)

Taro 06-01-2012 12:44 AM

What are some other ways to write JavaScript arrays?
 
Hello,

Are there other formats and configurations to write a JavaScript array? Note that there will be at least 100 pairs.

Code:

var thisArray = ["soda", "cake", "burger", "fries", "olive"];

var thatArray = ["aws1", "xzv2", "poi3", "gdf4", "lkj5"];

Each value must correspond to each other, and there can be no mismatch between values; the code will tend to be edited repeatedly after publishing.

In advance, I appreciate any help to this matter.

Old Pedant 06-01-2012 01:05 AM

Better:

Code:

function pair( a, b )
{
    this.left = a;
    this.right = b;
}

var pairs = [
    pair("soda","awsl"), pair("cake","xzv2"), pair("burger","poi3"), ...
  ];

// to iterate through them:
for ( var p = 0; p < pairs.length; ++p )
{
    var first = pairs[p].left;
    var second = pairs[p].right;
    ... do something with first and second ...
}
// (the names used there are of course entirely up to you)

Or, possibly even better:
Code:

// note the curly braces in place of square brackets!
pairs = {
    "soda" : "awsl",
    "cake" : "xzv2",
    "burger" : "poi3",
    ...
    };

// to iterate through them:
for ( var first in pairs )
{
    var second = pairs[first];
    ... do something with first and second ...
}



All times are GMT +1. The time now is 04:27 AM.

Powered by vBulletin®
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.