Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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}];

}
share|improve this question
you want taskGroup like [name, number]? – Nemoy Mar 15 '12 at 16:41
do you want taskgroup[name]=number? – Daniel Hunter Mar 15 '12 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] – Wesley Petrowski Mar 15 '12 at 16:49

2 Answers

up vote 0 down vote accepted

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];
share|improve this answer
how would i set the values of a task if i went with this.taskGroup = [ new task(name, number) ]; ?? – user1271792 Mar 15 '12 at 17:01
this.taskGroup[0].name = newName; or use this.taskGroup[0] = new task(name, number). If you want to add a new task, this.taskGroup.push(new task(name, number)); – villecoder Mar 15 '12 at 22:50

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};
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.