动态创建二维对象Javascript

时间:2016-10-03 07:59:40

标签: javascript arrays object

我有点棘手的情况,我无法找到一个好的解决方案。 我有一个以下类型的数组:

data = [ '1', '1', '964', '718', '0', '0', '0' ];

具体如下:

数据[0] =幻灯片编号

数据[1] =形状编号

data [2] = width

数据[3] =身高

data [4] = left

数据[5] =顶部

data [6] = href

解释

幻灯片可以有多个形状,每个形状都有5个属性(宽度,高度,左边,顶部,href)。

目标

我想创建一个可容纳所有数据的大对象。 像这样:

[{"Slide1" : [{
    "Shape1" : {
      "Width" : 200,
      "Height" : 100,
      "Left" : 200,
      "Top" : 100,
      "Link" : "1"
    }
  }, {
    "Shape2" : {
      "Width" : 200,
      "Height" : 100,
      "Left" : 200,
      "Top" : 100,
      "Link" : "0"
    }
  }]
}, {
  "Slide2" : [{
    "Shape1" : {
      "Width" : 200,
      "Height" : 100,
      "Left" : 200,
      "Top" : 100,
      "Link" : "0"
    }
  }, {
    "Shape2" : {
      "Width" : 200,
      "Height" : 100,
      "Left" : 200,
      "Top" : 100,
      "Link" : "1"
    }
  }, {
    "Shape3" : {
      "Width" : 200,
      "Height" : 100,
      "Left" : 200,
      "Top" : 100,
      "Link" : "2"
    }
  }]
}]

我写了下面的代码,几乎让我在那里,但仍然没有关闭:

for(var x = 0; x<data.length; x++){
    data[x] = data[x].slice(1, -1);
    data[x] = data[x].split("#");
}
var count = 0;
var total_slides = 66;
var slidesnr_and_shapesnr = {};
var data_object = {};
while(count < data.length)
{
    slidesnr_and_shapesnr[data[count][0]] = data[count][1];
    count++;
}
for(var i = 1; i<=total_slides; i++){
    for(var j = 1; j <= slidesnr_and_shapesnr[i]; j++){
        for(var k = 0; k<data.length; k++){
            if(data[k][0] == i && data[k][1] == j){
                data_object['slide'+i]['shapes'+j] = {
                    'width': data[k][2],
                    'height': data[k][3],
                    'left': data[k][4],
                    'top': data[k][5],
                    'href': data[k][6]
                }
            }
        }
    }
}

我得无法设置未定义的属性'shapes1'可能是因为:

data_object['slide'+i]['shapes'+j] // this line

哪种方式可以创建我想要的对象格式?

P.S。数据变量保存多个看起来相同的数组。是数组中的数组。 其他一些测试数据将是:

[ '1', '1', '964', '718', '0', '0', '0' ]
[ '2', '1', '964', '718', '0', '0', '0' ]
[ '2', '2', '311', '379', '612', '179', '0' ]
[ '2', '3', '35', '39', '36', '694', '0' ]
[ '2', '4', '35', '39', '75', '694', '0' ]
[ '3', '1', '964', '718', '0', '0', '0' ]
[ '3', '2', '116', '137', '46', '598', '16' ]
[ '3', '3', '35', '39', '181', '696', '0' ]
[ '3', '4', '35', '39', '220', '696', '0' ]
[ '3', '5', '35', '39', '259', '696', '0' ]
[ '3', '6', '35', '39', '297', '696', '0' ]
[ '3', '7', '35', '39', '337', '696', '0' ]
[ '3', '8', '51', '51', '658', '541', '0' ]
[ '3', '9', '51', '51', '787', '541', '0' ]
[ '4', '1', '964', '718', '0', '0', '0' ]
[ '4', '2', '116', '137', '46', '598', '62' ]
[ '4', '3', '35', '39', '181', '696', '0' ]
[ '4', '4', '35', '39', '221', '696', '0' ]
[ '4', '5', '35', '39', '260', '696', '0' ]
[ '4', '6', '35', '39', '298', '696', '0' ]
[ '4', '7', '56', '56', '894', '260', '0' ]
[ '5', '1', '964', '718', '0', '0', '0' ]
[ '5', '2', '116', '137', '46', '598', '24' ]
[ '5', '3', '35', '39', '181', '696', '0' ]
[ '5', '4', '35', '39', '298', '696', '0' ]
[ '5', '5', '35', '39', '221', '696', '0' ]
[ '5', '6', '35', '39', '260', '696', '0' ]

1 个答案:

答案 0 :(得分:2)

把它放在违规行前面:

if (!data_object['slide'+i]) {
  data_object['slide'+i] = {};
}