具有命名和编号索引的Javascript多维对象

时间:2017-12-14 10:55:18

标签: javascript object multidimensional-array

如何在javascript对象中存储此结构,尝试了很多方法,但无法找到完整的解决方案。

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 个答案:

答案 0 :(得分:1)

在分配属性之前,需要初始化每个对象。尝试添加此内容;

Scheduling[0]={};

在分配属性之前,您需要在整个过程中初始化每个嵌套对象和数组。

我更新了代码的第一部分,如下所示:

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);

答案 1 :(得分:1)

您永远不会将要分配MilestoneName的对象实例化,只有它的父级。

例如,您可以尝试这样做:

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))

答案 2 :(得分:0)

您还可以构建如下的嵌套对象:

var Scheduling = {};

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

我还建议使用数组,无论你在哪里处理增量索引:

var Scheduling = []; // array instead

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

答案 3 :(得分:0)

这是我的最终代码

 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));