473,471 Members | 1,814 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

How to put an array in an object?

I'm writing a little javascript program to help keep track voting logs
for a game of "werewolf".

I then tried to add an array to my object constructor and the code
went kablooey on me. I feel lost here.

//object constructor
function VoterRecord(name, angrymob, tally) {
this.name = name;
this.angrymob = angrymob;
this.tally = tally;

That constructor works if angrymob is just a simple variable, as shown
in the code snippet below:

i = 0;
village = new Array(50);
for (var i=0; i<49; i++)
village[i] = new VoterRecord("","",0);

But angrymob needs to be an array filled with the names of people
voting to lynch the person. Its when I try that (as in the code
below) it goes kablooey:

village = new Array(50);
mob = new Array(20);
for (var i=0; i<49; i++)
village[i] = new VoterRecord("",mob,0);

I really don't know the syntax here. How do you put an array in an
object? I'm lost.
Jul 23 '05 #1
2 1543
Lee
Kokolums said:

I'm writing a little javascript program to help keep track voting logs
for a game of "werewolf".

I then tried to add an array to my object constructor and the code
went kablooey on me. I feel lost here.

//object constructor
function VoterRecord(name, angrymob, tally) {
this.name = name;
this.angrymob = angrymob;
this.tally = tally;

That constructor works if angrymob is just a simple variable, as shown
in the code snippet below:

i = 0;
village = new Array(50);
for (var i=0; i<49; i++)
village[i] = new VoterRecord("","",0);

But angrymob needs to be an array filled with the names of people
voting to lynch the person. Its when I try that (as in the code
below) it goes kablooey:

village = new Array(50);
mob = new Array(20);
for (var i=0; i<49; i++)
village[i] = new VoterRecord("",mob,0);

I really don't know the syntax here. How do you put an array in an
object? I'm lost.

In general, it would be easier to help you if you gave more
detail that "it goes kablooey".

In this case, one problem is that you're assigning the same
array to every new VoterRecord. I don't think that's what
you want. Also, if you want 50 elements in your array,
you want i to loop from 0 to 49, not 48. Or simply use the
size of the array, to be more general:

village = new Array(50);
for (var i=0; i<village.length; i++) {
mob = new Array(20);
village[i] = new VoterRecord("",mob,0);
}

Jul 23 '05 #2
Lee <RE**************@cox.net> wrote in message news:<c6********@drn.newsguy.com>...

In general, it would be easier to help you if you gave more
detail that "it goes kablooey".

In this case, one problem is that you're assigning the same
array to every new VoterRecord. I don't think that's what
you want. Also, if you want 50 elements in your array,
you want i to loop from 0 to 49, not 48. Or simply use the
size of the array, to be more general:

village = new Array(50);
for (var i=0; i<village.length; i++) {
mob = new Array(20);
village[i] = new VoterRecord("",mob,0);
}


That's it! Keep Defining a new array in a loop over and over! That
fixed everything! Works like a charm now. Much thanks sir.
Jul 23 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: Mike S. Nowostawsky | last post by:
I tried using the "toUpperCase()" property to change the value of an array entity to uppercase BUT it tells me that the property is invalid. It seems that an array is not considered an object when...
58
by: jr | last post by:
Sorry for this very dumb question, but I've clearly got a long way to go! Can someone please help me pass an array into a function. Here's a starting point. void TheMainFunc() { // Body of...
5
by: Denis Perelyubskiy | last post by:
Hello, I need to make an array of elements accross forms. My javascript skills, as evident from this question, are rather rudimentary. I tried to make an associative array and index it with...
47
by: VK | last post by:
Or why I just did myArray = "Computers" but myArray.length is showing 0. What a hey? There is a new trend to treat arrays and hashes as they were some variations of the same thing. But they...
22
by: VK | last post by:
A while ago I proposed to update info in the group FAQ section, but I dropped the discussion using the approach "No matter what color the cat is as long as it still hounts the mice". Over the last...
35
by: VK | last post by:
Whatever you wanted to know about it but always were affraid to ask. <http://www.geocities.com/schools_ring/ArrayAndHash.html>
7
by: Robert Mark Bram | last post by:
Hi All! How do you get the length of an associative array? var my_cars= new Array() my_cars="Mustang"; my_cars="Station Wagon"; my_cars="SUV"; alert(my_cars.length);
38
by: VK | last post by:
Hello, In my object I have getDirectory() method which returns 2-dimentional array (or an imitation of 2-dimentional array using two JavaScript objects with auto-handled length property - please...
2
by: JJA | last post by:
I'm looking at some code I do not understand: var icons = new Array(); icons = new GIcon(); icons.image = "somefilename.png"; I read this as an array of icons is being built. An element of...
17
by: =?Utf-8?B?U2hhcm9u?= | last post by:
Hi Gurus, I need to transfer a jagged array of byte by reference to unmanaged function, The unmanaged code should changed the values of the array, and when the unmanaged function returns I need...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.