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

Managing unknowns 1

Status
Not open for further replies.

CassidyHunt

IS-IT--Management
Joined
Jan 7, 2004
Messages
688
Location
US
I have a web application that I can't seem to get over the hump of how to make it work.

I need to create an array of fields like this:

Code:
var fields = new Array('Qty','Description','Price','Weight');

Where the trick is is that I do not ever really know how many fields there are going to be and I need to use them to make an array of an item. Using the same example as above I would need this:

item 1
qty = 5
description = This is an item
Price = $25.00
Weight = 5lbs

Then I need to create an array of those items

group 1
item 1
qty = 5
description = This is an item
Price = $25.00
Weight = 5lbs
item 2
qty = 3
description = This is an item 2
Price = $2.00
Weight = 1lbs
group 2
item 1
qty = 5
description = This is an item
Price = $25.00
Weight = 5lbs
item 2
qty = 3
description = This is an item 2
Price = $2.00
Weight = 1lbs

Then I would need an array of the groups. I am struggling on how to make this all dynamic in javascript. I guess I do not understand how I would access the array structure.

Any ideas or direction would help.

Thanks

Cassidy
 
I'd start by not using array to hold the item data itself, opting for an object instead. You might want to use an array to hold all the objects, however:

Code:
function MyItem(qty, desc, price, weight) {
   this.qty = qty;
   this.description = desc;
   this.price = price;
   this.weight = weight;
}

var group1items = [];
group1items[0] = new MyItem(5, 'This is an item', 25, 5);
group1items[1] = new MyItem(3, 'This is an item 2', 2, 1);

var group2items = [];
group2items[0] = new MyItem(5, 'This is an item', 25, 5);
group2items[1] = new MyItem(3, 'This is an item 2', 2, 1);

alert(group1items[1].description);

Hope this helps,
Dan

[tt]Dan's Page [blue]@[/blue] Code Couch
[/tt]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top