Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Array size in JS 2

Status
Not open for further replies.

JayF82

Programmer
Jul 14, 2006
15
CA
Hi,

I was looking at some code snippets online and sometime I saw stuff such as :

var arrayName = new Array()

arrayName[Date] = 'aasdfasdfsdf';

Its an easy way of indexing dates that are associated with a string (such as a calendar) or something but that create HUGE arrays.

Does JS treat arrays differently than other programming languages so that this doesn't use a lot of ressources or will it just create a huge array in memory just as in any other languages?

Thanks,

 
Well after some testing with a fun for loop which crashed my browser I've decided it wasn't the way to go......
 
It sounds like you are trying to create an associative array of dates with textual references?
Like making making Christmas translate to 12/25/2006??

You might be better doing it like this:

Code:
var arrayName = new Array()
arrayName['Christmas'] = '12/25/2006';

alert(arrayName['Christmas']);

Or to store an actual date object into the associate array:
Code:
var Xmas=new Date()
Xmas.setFullYear(2006,11,25)
var arrayName = new Array()
arrayName['Christmas'] = Xmas;
alert(arrayName['Christmas']);

In the second example the date object Xmas is set to the date for Christmas then assigned to the associated array under the name Christmas. You could just as easily assign it with the default index number of 0 but using the text Christmas is easier in some instances to keep track of.


At my age I still learn something new every day, but I forget two others.
 
Well more or less, I want an associative array yes but need to search by date and perform some for loops on all elements. I was hoping that having the Date as the index would work as I saw a few example online that were using that method.

After some testing I figured it really wasn't practical as it created HUGE array, for something as simple as arrName[20060717] = 'today';

Right now I'm implementing it with a two dimensional array for my events so that arrName[0][0] is the date [0][1] is the associated string.

It seems like your method would be even better if I can do loop through the array elements...is it at all possible on non-integer based array?
 
What is the purpose of the loop?
You could use the text of the date as the array element reference if you need to get at it directly, but if you are looping through all of the elements then there is no need to use an associative array and you just loop through all elements of the array to find the match.

I am not certain as I have not tried but I would think that even if you assigned text references to the array that you could still reference by index number so if the array has 10 elements you could still loop from 0 to 9 referencing it by index rather than the associated name.


At my age I still learn something new every day, but I forget two others.
 
Hi niteowl,

Thanks for the replies, I'm looping through my array to find the dates which are in the current year and month so that I can display only those.

Its working ok with a two dimensional array as I loop through the array first colum [0] and if I get a match I can then use [1] to have the label associated with that date.

I tried doing
Code:
var test = new Array(2);
test['z'] = 'a';
test['y'] = 'b';
	
for(var i=0; i<test.length; i++)
	alert(test[i]);

It didn't work would have been nice though.
 
Oh well, it was something I had never tried so did not know for sure.

You COULD use a third dimension in your array or have two levels in the second dimension so the array is accessed via a norml numerical index and you look at the value in the second dimension for the date.

Or you have two arrays, one with the date as value and the other with the text as the value and keep the index the same for both.
Loop through array one 0 to x testing if the value matches the search and if it does you use the current index number to pull the value from array two.


At my age I still learn something new every day, but I forget two others.
 
I initially started with two arrays, but then merged them in one to tidy up the code a little.

I'll steer clear of 3 dimensional arrays for now :)

Thanks for helping, I appreciate it.
 
Well, you CAN loop through an array without the index but I do not know how to retrieve the text name of the array to do the comparison.

Code:
var test = new Array(2);
test['z'] = 'a';
test['y'] = 'b';
    
for(var i in test)
    alert(test[i]);

This will loop you through all elements in the array though there is no guarantee what order they will be in.
If however your array element is named by the date I do not know how you would access that property in the above loop, only the value. So you may still need the two dimensional array depending on how you need to access the values.
You could not in the above loop check for an array element named y, you could just loop through all of the elements and access the values. Not sure if this will work for what you need or not.

At my age I still learn something new every day, but I forget two others.
 
Well nuts, just figured it out after I posted. :)
i is the index key so you just look at i for the text name of the element.
Code:
var test = new Array(2);
test['z'] = 'a';
test['y'] = 'b';
    
for(var i in test)
    alert(i + " " + test[i]);

At my age I still learn something new every day, but I forget two others.
 
Eh, cool sometime its just there for us to see. That could prove useful but I've finished my calendar with a two dimensional array now. I might change it when I get more time though.

Thanks.
 
If using the numeric date as an index causes a huge array to be built, try converting it to a string first. That should create an associative array instead, which shouldn't cause the same problem.

Tracy Dryden

Meddle not in the affairs of dragons,
For you are crunchy, and good with mustard. [dragon]
 
Hi tsdragon,

Thanks for the reply, that would work and would be the way to go if I was to rewrite it not using multidimensional array. I couldn't use an associative array since I didn't know you could retrieve the index string until theniteowl's post.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top