homepage Welcome to WebmasterWorld Guest from 54.242.18.190
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member

Home / Forums Index / Code, Content, and Presentation / JavaScript and AJAX
Forum Library, Charter, Moderator: open

JavaScript and AJAX Forum

    
use multiple rrays or one multi-dimensional array
What's the best approach
Rominall




msg:4581393
 6:07 pm on Jun 5, 2013 (gmt 0)

I'm working on displaying information on states.
So I have one array = STATE NAMES
I can make a 2D array for infoType
but there's more info for infoType.

For example: StateNames: Alaska, Alabama, Arizona,....
InfoType: Motto: We dare defend our rights, North to the Future, God enriches
Flag: y, y, y

Now flag (infoType) also has info such as content: White with red cross = Alabama, Blue with seal = Alaska.

I'm going to referencing this information frequently. Would it be better to create one BIG array with all the information in it, or smaller arrays for the information stuff and when there's a sub-info type create 2 d arrays for those?

Some states may not have all infoTypes and not all info of InfoType will have entries.

No- I'm not really doing the state stuff above - it was the closest I could come for an example. :D

 

Rominall




msg:4581399
 6:22 pm on Jun 5, 2013 (gmt 0)

I should add that I have a small audience <100 but they will be accessing the page really frequently probably once every 5 minutes. I want performance to be optimal - that's really my question.

Fotiman




msg:4581400
 6:25 pm on Jun 5, 2013 (gmt 0)

Having 2 different arrays to maintain sounds like it might be making things harder to manage.

Have you considered using an object literal instead? For example:

var states = {
ALABAMA: {
abbr: "AL",
name: "Alabama",
motto: "We Dare Defend Our Rights"
},
ALASKA: {
abbr: "AK",
name: "Alaska",
motto: "North To The Future"
},
...
}


So you could then access them like:
states.ALASKA.motto

or access all of the states:

for (var state in states) {
console.log(states[state].abbr);
}

Rominall




msg:4581406
 6:50 pm on Jun 5, 2013 (gmt 0)

So how would I do a sub-category like
Motto: English: We dare....
Motto: Latin: Sum notta.... (no idea actually)

Is it better to make them EACH property or create a multidimensional array? Which will run faster?

I will have one property that is going to be difficult.
Type: [options = Sunny, Wet, Cloudy]
Options: {options = Years}

So let's say AL was Sunny in 1996, 1998, 1999, but it was Wet in 1996, 1997, 1998. You can see that it could have multiple Type options for multiple years.

Fotiman




msg:4581464
 8:45 pm on Jun 5, 2013 (gmt 0)


var SUNNY = {key: 1, value: "Sunny"},
WET = {key: 2, value: "Wet"},
CLOUDY = {key: 3, value: "Cloudy"};
var states = {
ALABAMA: {
abbr: "AL",
name: "Alabama",
motto: {
en: "We Dare Defend Our Rights",
latin: "Sum notta..."
},
years: [
{id: 1996, type: [SUNNY.key, WET.key]},
{id: 1997, type: [WET.key]},
{id: 1998, type: [SUNNY.key, WET.key]},
{id: 1999, type: [SUNNY.key]}
]
},
...
}

console.log(states.ALABAMA.motto.en);
console.log(states.ALABAMA.motto.latin);


That's just one example of how you could do it. In that example, years is an array of "year" objects, with each one consisting of an id (the actual year), and a type (which is an array of type keys, so we're not storing a bunch of duplicate text strings over and over). You could loop over the year array for a given state, and then loop over the type array for a given year, then match the key to the types defined, and output the corresponding value if you wanted. Many ways to skin this cat.

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / JavaScript and AJAX
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved