Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

How to create a javascript object runtime like following

task{
 timestamp:string;
tasklist: array of object
}
share|improve this question
2  
You have to be more specific. Please provide valid examples. –  thefourtheye Nov 4 '14 at 8:27

3 Answers 3

up vote 1 down vote accepted

You can use an object initialiser for that:

var task = {
    timestamp: "this is a string",
    tasklist: []
};

[] creates an array (an empty one). JavaScript doesn't have "array(s) of object," a standard array can hold anything. (JavaScript does, these days, have typed arrays for other things, like 8-bit integers, but not objects.)

share|improve this answer
var task = {
   timestamp: new Date().getTime(),
   tasklist: [new Object(), new Object()]

};

This could be one way to do it. Then you can get access to the property in this way:

task.tasklist[i...n]

You could also create an array like this:

var task = []

and then assign the timestamp as a property:

task.timestamp = //myTimestamp
share|improve this answer
var task = {};
task.timestamp = 'your string';
task.tasklist = [];

and then add task list objects like:

task.tasklist.push({'id': 5, 'name': 'task list 5'});

or:

var tasklistentry = {};
tasklistentry.id = 5;
tasklistentry.name = 'task list 5';
task.tasklist.push(tasklistentry);
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.