Jump to content

Array Loop problem


dfowler

Recommended Posts

Hey guys, I need some help.  Ok, here is the original array.  It contains different plans for a product we are offereing.

plansA = new Array(); 
plansA[0] = new Array(); 
plansA[0]['minutes'] = '500'; 
plansA[0]['price'] = '39.95'; 
plansA[0]['description'] = '1 Year; 500 minutes'; 
plansA[1] = new Array(); 
plansA[1]['minutes'] = '1000'; 
plansA[1]['price'] = '78.95'; 
plansA[1]['description'] = '1 Year; 1,000 minutes'; 
plansA[2] = new Array(); 
plansA[2]['minutes'] = '1500'; 
plansA[2]['price'] = '117.95'; 
plansA[2]['description'] = '1 Year; 1,500 minutes'; 
plansA[3] = new Array(); 
plansA[3]['minutes'] = '2000'; 
plansA[3]['price'] = '153.00'; 
plansA[3]['description'] = '1 Year; 2,000 minutes'; 

The user answers questions and ends up with a number ('total'), let's say 900.  In my script I use this loop:

for (var j in plansA) {
if (plansA[j]['minutes'] >= total) {
	var minutes = plansA[j]['minutes'];
	var price = plansA[j]['price'];
	var description = plansA[j]['description'];
	break;
	}
}

To get the plan that is closest to the number they end up with (in this case it would be the 1000 minute plan).  At the bottom I am giving the user the ability to compare plans.  So I need to get the information from the other plans so I can display them at the bottom.  I don't think I can do it in the first loop as there are multiple plans >= 900.  So I thought I could create a second loop that would only get the information on the other plans:

for (var j in plansA) {
if (plansA[j]['minutes'] != minutes) {
	var minutes2 = plansA[j]['minutes'];
	var price2 = plansA[j]['price'];
	var description2 = plansA[j]['description'];
  	}
}

My problem is that I know there will end up being 3 plans in the second loop.  I was hoping to have the var's named minutes2, minutes3, minutes4, price2, price3, price4, etc...  That way I can call them later in the code.  I couldn't figure out how to do that, so I tried to put everything into an array, like this:

min = newArray();
var i = 1;
for (var j in plansA) {
if (plansA[j]['minutes'] != minutes) {
	min[i]['minutes'] = plansA[j]['minutes'];
	min[i]['price']  = plansA[j]['price'];
	min[i]['description']  = plansA[j]['description'];
	i++;
  	}
}

However, whenever I try to call the variable (like min[1]['minutes']) I get 'undefined'.  Anybody know how I can do what I need to do?  Thanks!

Link to comment
Share on other sites

Ok, I figured it out myself.  I forgot to declare a new array!  I modified this:

min = new Array();
var i = 1;
for (var j in plansA) {
   if (plansA[j]['minutes'] != minutes) {
      min[i]['minutes'] = plansA[j]['minutes'];
      min[i]['price']  = plansA[j]['price'];
      min[i]['description']  = plansA[j]['description'];
      i++;
     }
}

into this:

min = newArray();
var i = 1;
for (var j in plansA) {
   if (plansA[j]['minutes'] != minutes) {
      min[i] = new Array();
      min[i]['minutes'] = plansA[j]['minutes'];
      min[i]['price']  = plansA[j]['price'];
      min[i]['description']  = plansA[j]['description'];
      i++;
     }
}

Link to comment
Share on other sites

Arrays in javascript aren't meant to be used that way, fyi.

Not like "associative arrays" that you'd find in php.

 

What you want to use are new Objects(),  or just {};

 

The difference is subtle, but arrays should have numeric keys only.

 

check out

http://godbit.com/article/js-tidbits-datatypes-object-object-vs-array

and search for similar articles.

 

beware of this, too:

a = []; // new array
a[10] = true; 
a.length; //  == 11 !

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.