0

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

data[1] = shape number

data[2] = width

data[3] = height

data[4] = left

data[5] = top

data[6] = href

Explanation

A slide can have multiple shapes, and each shape has the 5 attributes (width, height, left, top, 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:

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

Which would be the way I could create the object format I want?

P.S. The data variable is holding multiple arrays which look the same. 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' ]
1
  • The data structure you would like to construct seems to have one unnecessary nested object. You don't need an object with a single property called Shape#. I would simply make the Slide# array 2D instead. Commented Oct 3, 2016 at 9:10

1 Answer 1

2

Put this in front of the offending line:

if (!data_object['slide'+i]) {
  data_object['slide'+i] = {};
}
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.