0

How to store this structure in javascript object, tried many ways, but couldn't find a complete solution.

var Scheduling = new Object();

//first Milestone
Scheduling[0]['MilestoneName'] = "Milestone One";
    //Subtask List
    Scheduling[0]['SubTask'][0]['SubTaskName'] = "SubTask One";
    Scheduling[0]['SubTask'][0]['CompletionPercent'] = "50";

    Scheduling[0]['SubTask'][1]['SubTaskName'] = "SubTask Two";
    Scheduling[0]['SubTask'][1]['CompletionPercent'] = "80";

//Second Milestone
Scheduling[1]['MilestoneName'] = "Milestone Two";

    Scheduling[1]['SubTask'][0]['SubTaskName'] = "SubTask One";
    Scheduling[1]['SubTask'][0]['CompletionPercent'] = "20";

    Scheduling[1]['SubTask'][1]['SubTaskName'] = "SubTask Two";
    Scheduling[1]['SubTask'][1]['CompletionPercent'] = "15";


console.log(JSON.stringify[x]);

enter image description here

4 Answers 4

1

You need to initialize each object before assigning their properties. Try adding this;

Scheduling[0]={};

And you need to initialize each nested object and array along the way before assigning their properties.

I updated first part of your code as follows:

var Scheduling = new Object();


Scheduling[0] = {};


//first Milestone
Scheduling[0]['MilestoneName'] = "Milestone One";
//Subtask List
  
Scheduling[0]['SubTask'] = [];
Scheduling[0]['SubTask'][0] = {};
   
Scheduling[0]['SubTask'][0]['SubTaskName'] = "SubTask One";
Scheduling[0]['SubTask'][0]['CompletionPercent'] = "50";

Scheduling[0]['SubTask'][1] = {};
Scheduling[0]['SubTask'][1]['SubTaskName'] = "SubTask Two";
Scheduling[0]['SubTask'][1]['CompletionPercent'] = "80";

console.log(Scheduling);

Sign up to request clarification or add additional context in comments.

1 Comment

var Scheduling = new Object(); ought to be an Array by the looks of it; the difference can be important.
1

You're never instantiating the object you want to assign MilestoneName to, only it's parent.

You could, for example, try this:

var Scheduling = new Array(); // You're only using numbers as indexes, use an array!

//first Milestone
Scheduling.push({
    MilestoneName: "Milestone One",
    SubTask: [
        {
            SubTaskName: "SubTask One",
            CompletionPercent: 50,
        },
        {
            SubTaskName: "SubTask Two",
            CompletionPercent: 80,
        },
    ]
});

Scheduling.push({
    MilestoneName: "Milestone Two",
    SubTask: [
        {
            SubTaskName: "SubTask One",
            CompletionPercent: 20,
        },
        {
            SubTaskName: "SubTask Two",
            CompletionPercent: 15,
        },
    ]
});
console.log('Object: %o, String: %s', Scheduling, JSON.stringify(Scheduling))

Comments

0

You can also construct nested objects like this:

var Scheduling = {};

//first Milestone
Scheduling[0] = {
    'MilestoneName': "Milestone One",
    'SubTask': {
        0: {
            'SubTaskName': "SubTask One",
            'CompletionPercent': "20"
        },
        1: {
            // and so on ...
        }
    }
};

I would also recomend to use arrays instead, wherever you deal with incremental index:

var Scheduling = []; // array instead

//first Milestone
Scheduling[0] = {
    // .. like before
    'SubTask': [  //here also an array
        { 
            // task one
        },
        {
            // task two
        }
    ],

Comments

0

This is my final code

 var Scheduling = new Object();

//first Milestone
Scheduling[0] = {};
Scheduling[0]['MilestoneName'] = "Milestone One";
//Subtask List
Scheduling[0]['SubTask'] = {};
Scheduling[0]['SubTask'][0] = {};
Scheduling[0]['SubTask'][0]['SubTaskName'] = "SubTask One";
Scheduling[0]['SubTask'][0]['CompletionPercent'] = "50";

Scheduling[0]['SubTask'][1] = {};
Scheduling[0]['SubTask'][1]['SubTaskName'] = "SubTask Two";
Scheduling[0]['SubTask'][1]['CompletionPercent'] = "80";

//Second Milestone
Scheduling[1] = {};
Scheduling[1]['MilestoneName'] = "Milestone One";
//Subtask List
Scheduling[1]['SubTask'] = {};
Scheduling[1]['SubTask'][0] = {};
Scheduling[1]['SubTask'][0]['SubTaskName'] = "SubTask One";
Scheduling[1]['SubTask'][0]['CompletionPercent'] = "50";

Scheduling[1]['SubTask'][1] = {};
Scheduling[1]['SubTask'][1]['SubTaskName'] = "SubTask Two";
Scheduling[1]['SubTask'][1]['CompletionPercent'] = "80";

alert(JSON.stringify(Scheduling));

Comments

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.