homepage Welcome to WebmasterWorld Guest from
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member

Home / Forums Index / Code, Content, and Presentation / JavaScript and AJAX
Forum Library, Charter, Moderator: open

JavaScript and AJAX Forum

JS and Arrays
What about multiple arrays

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

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?




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

    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)

    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)

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

    Global Options:
     top home search open messages active posts  

    Home / Forums Index / Code, Content, and Presentation / JavaScript and AJAX
    rss feed

    All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
    Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
    WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
    © Webmaster World 1996-2014 all rights reserved