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 Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Multidimensional Array Problem 1

Status
Not open for further replies.

zinja

MIS
Nov 14, 2002
149
US
I am trying to create a multidimensional array and am having a problem. Here is my declaration of the arrays:

Code:
var item_department = new Array();
var item_price = new Array();
var item_tax = new Array();
var item_qty = new Array();
var item_copay = new Array();
var item_number = new Array();
var item_sum = new Array();
var item_subtotal = new Array();
var item_name = new Array();
var workspace = 1;
item_department[workspace] = new Array();
item_price[workspace] = new Array();
item_tax[workspace] = new Array();
item_qty[workspace] = new Array();
item_copay[workspace] = new Array();
item_number[workspace] = new Array();
item_sum[workspace] = new Array();
item_subtotal[workspace] = new Array();
item_name[workspace] = new Array();

Here is where I try to populate the arrays

Code:
			item_department[workspace][item_index]=itemdepartment;
				item_price[workspace][item_index]=itemprice;
				item_tax[workspace][item_index]=(itemprice*storetax);
				item_qty[workspace][item_index]=multiplier;
				item_copay[workspace][item_index]=itemcopay;
				item_subtotal[workspace][item_index]=fixeddecimal((item_price[workspace][item_index]  * item_qty[workspace][item_index])); 
				item_number[workspace][item_index]=itemnumber;
				item_name[workspace][item_index]=itemname;

What am I doing wrong here - I am getting the following error:

item_department[workspace] has no properties

Any ideas?



LJ Wilson

My personal saying - Just remember, it can always get worse, and usually will.
 
feherke,

Thanks for the reply, item_index is a variable that has global scope (as do the arrays).

Thanks!

LJ Wilson

My personal saying - Just remember, it can always get worse, and usually will.
 
item_index is a variable that has global scope

Yes - but what value does it hold at the time you are using it?

This would seem the perfect type of data for an object structure:

Code:
var items = new Array();
var itemIndex = 0;

items[itemIndex] = {
			dept:     itemdepartment,
			price:    itemprice,
			tax:      itemprice * storetax,
			qty:      multiplier,
			copay:    itemcopay,
			subtotal: fixeddecimal(itemprice * multiplier),
			number:   itemnumber,
			name:     itemname
		};

alert(items[0].name);
alert(items[0].price);

Hope this helps,
Dan



Coedit Limited - Delivering standards compliant, accessible web solutions

[tt]Dan's Page [blue]@[/blue] Code Couch
[/tt]
 
Dan,

That is cool and your right that seems alot better than the way I am doing it now (I wasn't aware of that type of data control) - I will use it now. One question - how will that work with another dimension added (for workspace). In other words, I will need to have multiple workspaces for the items object.

I will also research this on my own, I am new to Javascript, but everyday I learn something new and am loving it.

Thanks and have a star!


LJ Wilson

My personal saying - Just remember, it can always get worse, and usually will.
 
I wasn't aware of that type of data control

zinja, have a look at - that fully describes nesting and all available types of data you can use

-kaht

Lisa, if you don't like your job you don't strike. You just go in every day and do it really half-assed. That's the American way. - Homer Simpson
 
I will need to have multiple workspaces for the items object.

No problem. You can add an array into the object:

Code:
var items = new Array();
var itemIndex = 0;

items[itemIndex] = {
            dept:       itemdepartment,
            price:      itemprice,
            tax:        itemprice * storetax,
            qty:        multiplier,
            copay:      itemcopay,
            subtotal:   fixeddecimal(itemprice * multiplier),
            number:     itemnumber,
            name:       itemname[!],
            workspaces: new Array()[/!]
        };

items[0].workspaces[0] = 'Whatever';
items[0].workspaces[1] = 'Something else';

Hope this helps,
Dan



Coedit Limited - Delivering standards compliant, accessible web solutions

[tt]Dan's Page [blue]@[/blue] Code Couch
[/tt]
 
Great idea, I am now using the object notation to describe the item elements and it is much more efficient (and better looking). I think I didn't communicate the issue of workspace correctly - I need the workspace to hold the items. In other words, there will be multiple workspaces that could have items in each workspace.

I will continue to work on this, but if you have an idea for handling multiple workspaces, please let me know. I have learned more this week than the past couple.

Thanks for the help so far.


LJ Wilson

My personal saying - Just remember, it can always get worse, and usually will.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top