Click to See Complete Forum and Search --> : dynamic access array elements
Hi,
I was wondering how I could dynamically access an element in a javascript array, without having to make arrays within an array.
**I fill the customer array
var numr = 1;
cust[numr++] = new cust ("Joe","Doe","Doe Town");
cust[numr++] = new cust ("Lee","Main","Leeville");
numr --; // nr of rows
numrcols = cust.length; // nr of columns
**array definition
function cust(firstname,lastname,city)
{
this.firstname = firstname
this.lastname = lastname
this.city = city
}
** and now I want to put the array on screen (markup of the text is not important in this example)
** this works
for (var a=1; a <= numr ; a++)
{
document.write(customer[a].lastname + ", ");
document.write(customer[a].firstname + "<br>");
}
** but how must I write this one, without having to give the name of the element ?
for (var a=1; a <= numr ; a++)
{
for (var b=1; b <= numrcols ; b++)
{
document.write(customer[a].element[b] +", ") // WRONG
}
}
Thanks,
Bart
wfsaxton
02-17-2003, 09:08 AM
I guess I'm having trouble understanding why the last one is wrong? Could you give an example of why its not doing what you need to have it do?
Also, why the aversion to arrays? Just curious.
Charles
02-17-2003, 09:30 AM
I think that this might be what you are seeking. I've changed you names a bit to follow the Java naming conventions which JavaScript follows. (http://java.sun.com/docs/codeconv/html/CodeConventions.doc8.html)
<pre>
<script type="text/javascript">
<!--
// object constructor for the Customer class
function Customer (firstName,lastName,city) {
this.firstName = firstName;
this.lastName = lastName;
this.city = city;
}
// methods inherited by all Customer objects
Customer.prototype.getName = function () {return this.lastName + ', ' + this.firstName}
Customer.prototype.getCity = function () {return this.city}
Customer.prototype.toString = function () {return [this.firstName, this.lastName, 'of', this.city].join(' ')}
// create and array of Customer objects
var cust = [new Customer('Joe','Doe','Doe Town'), new Customer ('Lee','Main','Leeville')];
for (var i=0; i<cust.length; i++) {document.writeln (cust[i].getName())};
document.writeln();
for (var i=0; i<cust.length; i++) {document.writeln (cust[i])};
// -->
</script>
</pre>
You replied with
cust[i].getName())
in this case, I still have to use (in one way or another) the name of the 'field' I want to have
I only want to access DYNAMICALLY a certain element of an array.
I don't want to say: from the third 'record' give me the 'lastname field', but rather:
from the thrid 'record' give me the value of the second 'field'.
It's just the approach that is interesting to me.
The array given above was a mere example. I can think of a lot of programs were this could come in handy, but I only find references on the web and in books about 'records containing 1 field' and not several fields saying: arrayelement[x] and nothing about arrayelement[x].field[y]
writing arrays within arrays is not an option for me
something like:
customer[1] = new array (new array "Joe", new array "Doe")
Though this allows me to say: customer[1][2]
khalidali63
02-17-2003, 09:58 AM
In my understanding I have never seen a user defined object (cust) keep its members record indexed as you are trying to do.
When you create an object and then instantiate it it will have all the member methods and variables to be used only when referenced by there name.
I have neveer seen anything being instantiated the way you are trying to do.
Khalid
Charles
02-17-2003, 10:05 AM
You are creating objects, not arrays. And objects are not indexed like arrays are.
as for select lists e.g. this.document.forms[0].dropdownlist.options[this.document.forms[0].dropdownlist.selectedIndex].value, I thought that objects/arrays could be addressed also in such a way
I can imagine the browser internally does that too
The only difficulty to me seemed to find the correct argument
Thanks