Javascript对象数组,其中对象也包含数组

时间:2015-06-27 10:29:44

标签: javascript arrays object

我是Javascript和编程的新手,我希望有人可以帮助我。

我正在尝试创建一个模型,药物在不同的隔间/器官中分布在整个身体。隔间有药物流入和药物从另一个隔室流出。

我想创建一个名为body的变量,它包含一个对象数组,也就是不同的隔间/器官,我可以访问各种参数。

我可以创建以下

 var mybody = 
[
    {ID:"1", fullname:"Arterial", inflowID:"0", outflowID:"2"},
    {ID:"2", fullname:"Kidney",   inflowID:"1", outflowID:"3"},
    {ID:"3", fullname:"Vein",     inflowID:"2", outflowID:"0"},
];

我想要实现的是inflowID需要是大小为2的数组,inflowID [0],inflowID [1],inflowID [2]。

以下语法dosnt工作,但你可以看到我想要实现的目标

{ID:"1", fullname:"Arterial", inflowID[0]:"3", inflowID[1]:"4", inflowID[2]:"nil", outflowID:"2"},

我尝试过以各种方式使用方括号,卷曲括号和曲线括号,但无法确定正确的语法需要。

如果我需要,我可以将其从我的身体阵列中分离出来。

大卫

3 个答案:

答案 0 :(得分:0)

你在这里:

{ID:"1", fullname:"Arterial", inflowID:["3","4","nil"], outflowID:"2"}

答案 1 :(得分:0)

构建数组就像这样简单:

[ element 1, element 2, element 3, ... , element n ]

您的对象数组已经正确。

var mybody = [
    {ID:"1", fullname:"Arterial", inflowID:["0", "0", "0"], outflowID:"2"},
    {ID:"2", fullname:"Kidney",   inflowID:["1", "1", "1"], outflowID:"3"},
    {ID:"3", fullname:"Vein",     inflowID:["2", "2", "2"], outflowID:"0"},
];

答案 2 :(得分:0)

试试这个可能对你有所帮助...... 示例..

var mybody = 
    [
        {
             ID:"1", 
             fullname:"Arterial", 
             inflowID:["3","4","nil"], 
             outflowID:"2"
       },
        {
             ID:"2", 
             fullname:"Kidney",   
             inflowID:["2","1","0"], 
             outflowID:"3"
        },
        {
             ID:"3", 
             fullname:"Vein",     
             inflowID:["3","3","0"], 
             outflowID:"0"
       },
    ];