How to put an array in an object?

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

    #1

    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(nam e, 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.
  • Lee

    #2
    Re: How to put an array in an object?

    Kokolums said:[color=blue]
    >
    >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(nam e, 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.[/color]


    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.lengt h; i++) {
    mob = new Array(20);
    village[i] = new VoterRecord("", mob,0);
    }

    Comment

    • Kokolums

      #3
      Re: How to put an array in an object?

      Lee <REM0VElbspamtr ap@cox.net> wrote in message news:<c6bdhs0dg v@drn.newsguy.c om>...
      [color=blue]
      >
      > 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.lengt h; i++) {
      > mob = new Array(20);
      > village[i] = new VoterRecord("", mob,0);
      > }[/color]

      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.

      Comment

      Working...