0

I want to create this object but i'm not sure how to create it with the taskGroup variable as an array. This was as far as i could get with it.

function listItem(name, number) {
    this.name = name;
    this.number = number;

    this.taskGroup = taskGroup; 
}

function taskGroup(name, number) {
    taskGroup = [{name, number}];

}
3
  • you want taskGroup like [name, number]? Commented Mar 15, 2012 at 16:41
  • do you want taskgroup[name]=number? Commented Mar 15, 2012 at 16:43
  • I'm not sure what you're asking here. 1) "I want to create this object" - which object are you talking about? Do you want to create a new listItem? 2) You've got a function called taskGroup, a variable inside that function called taskGroup, and a variable in listItem called taskGroup. I'm not sure which taskGroup you are talking about in your question. 3) Do you want to create an array out of your name and number variables? The syntax for that is myArray = [name, number] Commented Mar 15, 2012 at 16:49

2 Answers 2

0

Perhaps you need to rethink how you're creating a task group. Rather than have a taskGroup object, have a task object. Then your taskGroup member in listItem becomes an array of tasks.

function task(name, number) {
   this.name = name;
   this.number = number;
}

function listItem(name, number) {
   this.name = name;
   this.number = number;

   //Don't do both of these -- choose which is appropriate
   //Create an empty array
   this.taskGroup = new Array();

   //or Create an array with one task already defined based on this name and number
   this.taskGroup = [ new task(name, number) ];
}

Then you may access your taskGroup variable like so:

var item = new listItem('one', 1);
var firstTask = item.taskGroup[0];
1
  • how would i set the values of a task if i went with this.taskGroup = [ new task(name, number) ]; ?? Commented Mar 15, 2012 at 17:01
0

There is a syntax error. This is a correct array:

taskGroup = [name, number];

Patterns are:

var my_tab = [one, two, three];

var my_object = {one: 1, two: 2, three: 3};

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.