Welcome to WebmasterWorld Guest from

Forum Moderators: open

Message Too Old, No Replies

JS and Arrays

What about multiple arrays



6:07 pm on Feb 5, 2002 (gmt 0)

10+ Year Member

Hi all,
I want to ask you all a question. How can I go about setting up one or two arrays.

This is what I want

Lunchbox1 -

  • White Milk
  • Bolonie Sandwhich
  • Bannana

    Lunchbox2 -

  • Chocolate Milk
  • Ham & Cheese Sandwich
  • Apple

    Lunchbox3 -

  • Coca Cola
  • Hamburger
  • French Fries

    Now I want to take Lunchbox1, lunchbox2 and lunchbox3 and put it in an array. Then I want to grab there contents and put them into an array. Now the question here is do I need to do them as array1 - for drinks, array2 - for sandwich and array3 - for dessert. Or is there an esier way of doing this?


  • tedster

    10:02 pm on Feb 5, 2002 (gmt 0)

    WebmasterWorld Senior Member tedster is a WebmasterWorld Top Contributor of All Time 10+ Year Member

    Can you depend on always defining the array elements in the same order?

    If so, you might get away with one generic "food" array. You need to be sure that the 1modulo3 element is always a beverage, 2modulo3 is always a sandwich, and 0modulo3 is always a sidedish. But that might be prone to errors - you're depending on an ordering convention that might not always be true down the line.

    Alternately, if each of the sub-groups have some dependable characteristic, you could define all the elements not worrying about the order -- and then use the array sort() method to group all your beverages in the first third of the array positions, sandwiches in the second third, etc. This is still prone to error, unless you have strict control of the elements.

    Long run, it's probably safer and clearer for future maintenance to create separate arrays for each of beverage, sandwich and sidedish. IMHO, of course.


    6:48 am on Feb 6, 2002 (gmt 0)

    10+ Year Member

    tedster has some good points, but I couldn't resist using a little OOP:


    var cafe = new Array();

    function Lunchbox(drink,sandwich,side) {
    this.drink = drink;
    this.sandwich = sandwich;
    this.side = side;
    cafe[cafe.length] = this;

    function createLunchboxes() {
    new Lunchbox("White Milk","Bolonie Sandwhich","Bannana");
    new Lunchbox("Chocolate Milk","Ham & Cheese Sandwich","Apple");
    new Lunchbox("Coca Cola","Hamburger","French Fries");

    function displayLunchboxes() {
    for (var i = 0; i < cafe.length; ++i) {
    "Lunchbox " + i + ":<br>" +
    "Drink: " + cafe[i].drink + "<br>" +
    "Sandwich: " + cafe[i].sandwich + "<br>" +
    "Side: " + cafe[i].side + "<br><br>"




    12:06 am on Feb 7, 2002 (gmt 0)

    WebmasterWorld Senior Member tedster is a WebmasterWorld Top Contributor of All Time 10+ Year Member

    From this thread [webmasterworld.com] it looks like circuitjump has it nailed now!

    Featured Threads

    Hot Threads This Week

    Hot Threads This Month