Multidimensional arrays help?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Ant

    Multidimensional arrays help?

    Hi,

    I'm trying to store some user input from a web form into an array, now the
    thing is I would like to store multiple users entries so I was thinking of
    using a multidimensiona l array so I might have something like this.

    userArray[0][0] would hold user 0's UserID
    userArray[0][1] would hold user 0's Name
    and
    userArray[1][0] would hold user 1's UserID

    etc.

    So I've been googling around for how to do this but have't come up with a
    way of doing multidimensoina l arrays in javascript, is it actually possible?
    I've seen ways of using associative arrays to achieve multidimensiona l
    properties but I'm not too sure how to do it.

    Hope someone can help!

    Thanks


  • Hal Rosser

    #2
    Re: Multidimensiona l arrays help?


    "Ant" <nospamf@nospam .com> wrote in message
    news:cubj9e$lm0 $1@wisteria.csv .warwick.ac.uk. ..[color=blue]
    > Hi,
    >
    > I'm trying to store some user input from a web form into an array, now the
    > thing is I would like to store multiple users entries so I was thinking of
    > using a multidimensiona l array so I might have something like this.
    >
    > userArray[0][0] would hold user 0's UserID
    > userArray[0][1] would hold user 0's Name
    > and
    > userArray[1][0] would hold user 1's UserID
    >
    > etc.
    >[/color]
    I'm not sure, but this may be a way to simulate 2-dimensional array -
    Use an array of arrays.
    userArray[0] = new Array(12345, "Jim Smith");
    userArray[1] = new Array(45678, "Joe Schmo");


    Comment

    • Ant

      #3
      Re: Multidimensiona l arrays help?

      "Hal Rosser" <hmrosser@bells outh.net> wrote in message
      news:3AcOd.694$ Tn6.510@bignews 3.bellsouth.net ...[color=blue]
      >
      > "Ant" <nospamf@nospam .com> wrote in message
      > news:cubj9e$lm0 $1@wisteria.csv .warwick.ac.uk. ..[color=green]
      >> Hi,
      >>
      >> I'm trying to store some user input from a web form into an array, now
      >> the
      >> thing is I would like to store multiple users entries so I was thinking
      >> of
      >> using a multidimensiona l array so I might have something like this.
      >>
      >> userArray[0][0] would hold user 0's UserID
      >> userArray[0][1] would hold user 0's Name
      >> and
      >> userArray[1][0] would hold user 1's UserID
      >>
      >> etc.
      >>[/color]
      > I'm not sure, but this may be a way to simulate 2-dimensional array -
      > Use an array of arrays.
      > userArray[0] = new Array(12345, "Jim Smith");
      > userArray[1] = new Array(45678, "Joe Schmo");[/color]

      Ah, I forgot to mention that there si about 10 fields of user info; name,
      address, telephone number etc

      and what I would like to happen is one set of useres data is entered using
      the form, a button is clicked and all their data is stored in an array, then
      the next set of user data can be typed into web form and then added to the
      array. I think the problem is that the number of users cannot be known in
      advance which is why I don't think the method you described can be used, or
      can it, maybe I'm being a bit slow?

      Cheers


      Comment

      • pjryanSpamless@gmail.com

        #4
        Re: Multidimensiona l arrays help?

        Hal's process will work for you. You can just extend his concept.

        So first define your user array with only one item:
        var userArray = new Array(1)

        Next, if each user will have 10 peices of data, define the first
        element of your user array something like:
        userArray[0] = new Array(10)

        Then you can access each position like this:
        userArray[0][0] = 123 //user id
        userArray[0][1] = "Jim" //name
        userArray[0][2] = "123 Elm" //address
        etc...

        Now when you've got a new user, you'll need to increase the size of
        your user array. And you know you're going to be adding a new user
        with another 10 elements so write:
        userArray.push( new Array(10))

        Now you can add a second user:
        userArray[1][0] = 999 //user id
        userArray[1][1] = "Sally" //name
        userArray[1][2] = "999 Elm" //address


        I don't fully understand your implementation of this, but it seems that
        each time you added a user, you could run a function that "pushed" the
        array, then added the data.

        Hope that helps a bit.

        Comment

        • Ant

          #5
          Re: Multidimensiona l arrays help?

          <pjryanSpamless @gmail.com> wrote in message
          news:1107910807 .952522.230990@ o13g2000cwo.goo glegroups.com.. .[color=blue]
          > Hal's process will work for you. You can just extend his concept.
          >
          > So first define your user array with only one item:
          > var userArray = new Array(1)
          >
          > Next, if each user will have 10 peices of data, define the first
          > element of your user array something like:
          > userArray[0] = new Array(10)
          >
          > Then you can access each position like this:
          > userArray[0][0] = 123 //user id
          > userArray[0][1] = "Jim" //name
          > userArray[0][2] = "123 Elm" //address
          > etc...
          >
          > Now when you've got a new user, you'll need to increase the size of
          > your user array. And you know you're going to be adding a new user
          > with another 10 elements so write:
          > userArray.push( new Array(10))
          >
          > Now you can add a second user:
          > userArray[1][0] = 999 //user id
          > userArray[1][1] = "Sally" //name
          > userArray[1][2] = "999 Elm" //address
          >
          >
          > I don't fully understand your implementation of this, but it seems that
          > each time you added a user, you could run a function that "pushed" the
          > array, then added the data.
          >
          > Hope that helps a bit.[/color]

          Thanks, that helps more than a bit!

          Ta


          Comment

          • Lee

            #6
            Re: Multidimensiona l arrays help?

            Ant said:[color=blue]
            >
            >"Hal Rosser" <hmrosser@bells outh.net> wrote in message
            >news:3AcOd.694 $Tn6.510@bignew s3.bellsouth.ne t...[color=green]
            >>
            >> "Ant" <nospamf@nospam .com> wrote in message
            >> news:cubj9e$lm0 $1@wisteria.csv .warwick.ac.uk. ..[color=darkred]
            >>> Hi,
            >>>
            >>> I'm trying to store some user input from a web form into an array, now
            >>> the
            >>> thing is I would like to store multiple users entries so I was thinking
            >>> of
            >>> using a multidimensiona l array so I might have something like this.
            >>>
            >>> userArray[0][0] would hold user 0's UserID
            >>> userArray[0][1] would hold user 0's Name
            >>> and
            >>> userArray[1][0] would hold user 1's UserID
            >>>
            >>> etc.
            >>>[/color]
            >> I'm not sure, but this may be a way to simulate 2-dimensional array -
            >> Use an array of arrays.
            >> userArray[0] = new Array(12345, "Jim Smith");
            >> userArray[1] = new Array(45678, "Joe Schmo");[/color]
            >
            >Ah, I forgot to mention that there si about 10 fields of user info; name,
            >address, telephone number etc
            >
            >and what I would like to happen is one set of useres data is entered using
            >the form, a button is clicked and all their data is stored in an array, then
            >the next set of user data can be typed into web form and then added to the
            >array. I think the problem is that the number of users cannot be known in
            >advance which is why I don't think the method you described can be used, or
            >can it, maybe I'm being a bit slow?[/color]

            That would be how you would add a row at a time of data.
            A more important question is what do you intend to do with this
            data once you've collected it. You can't submit an array via a
            form. It will vanish when the user leaves the page.

            Comment

            • Douglas Crockford

              #7
              Re: Multidimensiona l arrays help?

              >>>I'm trying to store some user input from a web form into an array, now[color=blue][color=green][color=darkred]
              >>>the
              >>>thing is I would like to store multiple users entries so I was thinking
              >>>of
              >>>using a multidimensiona l array so I might have something like this.
              >>>
              >>>userArray[0][0] would hold user 0's UserID
              >>>userArray[0][1] would hold user 0's Name
              >>>and
              >>>userArray[1][0] would hold user 1's UserID
              >>>
              >>>etc.
              >>>[/color]
              >>
              >>I'm not sure, but this may be a way to simulate 2-dimensional array -
              >>Use an array of arrays.
              >>userArray[0] = new Array(12345, "Jim Smith");
              >>userArray[1] = new Array(45678, "Joe Schmo");[/color]
              >
              >
              > Ah, I forgot to mention that there si about 10 fields of user info; name,
              > address, telephone number etc[/color]

              That is a very bad way to use arrays. It is much smarter to use objects
              for this kind of data.

              userData = [
              {id: 12345, name: "Jim Smith", address: ...},
              {id: 45678, name: "Joe Schmo", address: ...}]

              Use the right data structures. See

              Comment

              Working...