Multidimensional arrays help?

A

Ant

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 multidimensional 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 multidimensoinal arrays in javascript, is it actually possible?
I've seen ways of using associative arrays to achieve multidimensional
properties but I'm not too sure how to do it.

Hope someone can help!

Thanks
 
H

Hal Rosser

Ant said:
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 multidimensional 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.
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");
 
A

Ant

Hal Rosser said:
Ant said:
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 multidimensional 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.
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");

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
 
G

Guest

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.
 
L

Lee

Ant said:
Hal Rosser said:
Ant said:
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 multidimensional 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.
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");

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?

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.
 
A

Ant

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.

Thanks, that helps more than a bit!

Ta
 
D

Douglas Crockford

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 multidimensional 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.

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");


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

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
http://www.crockford.com/javascript/survey.html
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top