View Full Version : Array Help - Wish I could be more specific

Confused Idiot
10-25-2006, 12:36 AM
Hello, I'm new here and was hoping for a little help with something. I have no background in programming anything, so please don't shoot me down for using bad terms or thinking in a completely wrong way.

I've been reading parts of tutorials and other peoples' code in order to get a basic grasp of the language, and have run into the problem which follows.

One array is like this:

typ = new Array();
typ[0] = ("One");
typ[1] = ("Two");
typ[2] = ("Three");

Another is like this:

var item = new Array();
function obj_item(name, type)
//^Questioning the above - does obj_xx need to be the same as the array? Remember, I'm mostly learning from other peoples' code.
this.name = name;
this.type = type;
item[0] = new obj_ar2("Item1",1);

What I'd like to do is have "type" from the "item" array display what is in the "typ" array.

document.write(item[0].type);just returns "1", and I'd like to know how to return "Two". So I'm getting the right value, I just don't know how to use that value to write what is in the other array.

I've been trying things for hours, and searched all over the place, but not really knowing what I'm doing doesn't help as far as search terms go. Hopefully someone will understand what I'm trying to say/do.

Thanks a lot for any help, please remember I'm quite clueless and will probably be asking more questions in future.

10-25-2006, 01:03 AM
alright first of all you only have one array.


thats your array.

now an array is a list right? the things in the list are "elements".

so item[0] is an element and item[1] is another element.

now when you make an array its best to say how long the list is so you go

var item=new Array(12);

now you have an array with 12 elements from 0 to 11. Now we put things inside the array. Your using objects and i really dont think you should be doing that because you probably dont know what an object is. so we will just put some words in the array


if you want two you go document.writeln(item[1]);

Now are you just wanting to know this or do you have something your trying to make?

Hope that helps

Confused Idiot
10-25-2006, 01:19 AM
Thank you for the reply.

I'm trying to make a fairly advanced calculator based on an MMORPG, so I left out the specifics. I'm not too sure I can do it, but I'm having a lot of fun trying :).

Those aren't my exact arrays, I just simplified them as it was only an example. I'm not absolutely sure what an object is, but I do think I have a basic understanding of it. I'll try to explain what I'm trying to do and why I was using objects:

Say, for example, my item array is a list of dishes in a cookery book. Each dish is of a certain type - let's keep it simple and say food or drink. In the typ array, I have two elements set to food and drink respectively. Inside the item array, I define "type" as 0 or 1 - for food or drink. Now, my problem is displaying the words in the typ array instead of the numbers in the item array.

So using the example above, I'll also mention that I have a list of ingredients - let's say the array is named "ingr". I have a tomato listed as the first element in that array, and there are 4 tomatoes in one of the dishes. I believe this is done by using something like [0, 4] in the item array (or should that be object?), but I haven't gotten to that yet. This part is my main reason for using objects, so that's why I mentioned it.