Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 06-01-2012, 12:44 AM   PM User | #1
Taro
Regular Coder

 
Taro's Avatar
 
Join Date: Oct 2011
Location: Geraldton, Ontario
Posts: 155
Thanks: 1
Thanked 1 Time in 1 Post
Taro is an unknown quantity at this point
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.
__________________
Element ID

Webs Support Helper

Your friendly neighborhood Taroman.

Last edited by Taro; 06-01-2012 at 01:11 AM..
Taro is offline   Reply With Quote
Old 06-01-2012, 01:05 AM   PM User | #2
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,162
Thanks: 59
Thanked 3,992 Times in 3,961 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
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 ...
}
__________________
An optimist sees the glass as half full.
A pessimist sees the glass as half empty.
A realist drinks it no matter how much there is.
Old Pedant is offline   Reply With Quote
Reply

Bookmarks

Tags
array, configuration, format, publish

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 09:12 AM.


Advertisement
Log in to turn off these ads.