0

I'm trying to dynamically create javascript objects using bracket notation as demonstrated by the top answer here. I would hope to end up with similar objects to the following (though simply putting this javascript in gets an error on the bracket after sections as an "Unexpected Token"):

tabs[1]={
     sections[1]={
          children[1]={
               title:"Child1"
          }
          children[2]={
               title:"Child2"
          }
          children[3]={
               title:"Child3"
          }
     sections[2]={
          children[1]={
               title:"Child1"
          }
          children[2]={
               title:"Child2"
          }
     }
};
tabs[2]={
     sections[1]={
          children[1]={
               title:"Child1"
          }
          children[2]={
               title:"Child2"
          }
     sections[2]={
          children[1]={
               title:"Child1"
          }
          children[2]={
               title:"Child2"
          }
     }
     sections[3]={
          children[1]={
               title:"Child1"
          }
          children[2]={
               title:"Child2"
          }
     }
};

Is a structure like this achievable? My attempt at achieving it is lower on the page. How can I make the above structure happen for my situation? With it, would I be able to do something like:

   var childTitle = tabs[1].sections[2].child[2].title

These objects will be created from arrays that are created from csv files (using jquery-csv. The elements in the arrays/csvs are tab names, side-nav section names, and side-nav-subsection names. I need to create them dynamically because these arrays are subject to change as the number and names of tabs and sections within the app change. Here is my attempt:

    this.tabData = tabsService.tabData;
    var tabCount = tabsService.tabData.length;
    var tabs={};

    for (tCounter = 0; tCounter<tabCount; tCounter++){


        tabs[tCounter] ={
            var tabURL ="Contents/Product Groups/"+this.tabData[tCounter]+"/sectionOrder.csv";  
            var TabSectionData = $.getCsvArray(tabURL);
            var sectionCount = TabSectionData.length;


            for (sCounter = 0; sCounter<sectionCount; sCounter++){
                var tabChildURL ="Contents/Product Groups/"+this.tabData[tCounter]+"/"+TabSectionData[sCounter]+"/order.csv";
                var TabChildData = $.getCsvArray(tabChildURL);

                sections[sCounter] ={
                    "title" = TabSectionData.[sCounter];
                    cCounter = 0;
                    for (cCounter = 0; cCounter<TabChildData.length; cCounter++){
                            children[cCounter]={
                            "title": TabChildData.[cCounter];

                            }
                        }
                    }
                }
        }

As a reference, the arrays in the code above may look something like:

tabData=[Tab1, Tab2];
tabSectionData=[Section1, Section2];
tabChildData=[Child1, Child2, Child3];

though they are dynamically created everytime a csv file is read and subject to change.

Eventually, these elements will be repeated using AngularJS's ng-repeat similar to this post.

I attempted using arrays, but it ended up getting pretty complex and I couldn't get elements from a 3D array to repeat properly with angular. To see the navigation structure I'm going for and details on this attempt, please see this post I made yesterday.

The backend of the app updates the app.data folder on the users machine to have a specific folder structure with all of the app's content inside. The front end uses the csv files in these folders to navigate this folder structure and know the proper order to display content.

Thank you very much for your help. Your time is very appreciated. Let me know if there is anything more you need from me.

Community
  • 1
  • 1
user95227
  • 1,853
  • 2
  • 18
  • 36
  • 1
    You should get into the habit to start counting at `0`. Makes things a lot easier when you use a system that does the same. Anyway, the way you are declaring your objects is simply not correct. Luckily, the MDN JavaScript tutorial explains everything you need to know about objects and object literals: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Working_with_Objects – Felix Kling Feb 05 '15 at 17:09
  • What is the question?! I found one question there - "Is a structure like this achievable?". If this is the question, then the answer is "yes" – New Dev Feb 05 '15 at 18:08
  • @NewDev sorry. If it is possible, I wondering how I can get there. Thanks for looking into this post! – user95227 Feb 05 '15 at 18:17
  • @user95227, I suggest then you edit the question and put the input data structure, desired output structure (you already have that, but pls use 0-based index), and code that doesn't work. Also, remove unrelated tags, like AngularJs – New Dev Feb 05 '15 at 18:19

0 Answers0