简体   繁体   English

动态创建二维对象Javascript

[英]Dynamically create 2 dimensional object Javascript

I have kinda a tricky situation where I can't figure out a good solution. 我有点棘手,无法找到好的解决方案。 I have an array of the following type: 我有以下类型的数组:

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

Which is as follows: 如下:

data[0] = slide number 数据[0] =幻灯片编号

data[1] = shape number 数据[1] =形状编号

data[2] = width 数据[2] =宽度

data[3] = height 数据[3] =高度

data[4] = left 数据[4] =左

data[5] = top 数据[5] =顶部

data[6] = href 数据[6] = href

Explanation 说明

A slide can have multiple shapes, and each shape has the 5 attributes (width, height, left, top, href). 一张幻灯片可以有多个形状,每个形状都有5个属性(宽度,高度,左侧,顶部,href)。

Goal 目标

I want to create a big object which would hold all the data. 我想创建一个可以容纳所有数据的大对象。 Something like this: 像这样:

[{"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"
    }
  }]
}]

I wrote the following code, which almost got me there but, still not close: 我编写了以下代码,几乎可以将我带到那里,但仍然没有结束:

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]
                }
            }
        }
    }
}

I get Cannot set property 'shapes1' of undefined probably because: 无法设置未定义的属性'shapes1',可能是因为:

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

Which would be the way I could create the object format I want? 我将以哪种方式创建所需的对象格式?

PS The data variable is holding multiple arrays which look the same. PS数据变量包含多个看起来相同的数组。 Is an array within an array. 是数组中的一个数组。 Some other data for testing would be: 用于测试的其他一些数据是:

[ '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' ]

Put this in front of the offending line: 将其放在违规行的前面:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM