简体   繁体   English

如何在其他对象中动态生成javascript对象。 可以在对象内部使用for循环吗?

[英]How to dynamically generate javascript objects within other objects. Can you use a for loop inside an object?

I hope to achieve a set of javascript objects something like this: 我希望实现一组javascript对象,如下所示:

tabs[0]={
 sections[0]={
      title:"section0",
      children[0]={
           title:"Child0"
      },
      children[1]={
           title:"Child1"
      },
      children[2]={
           title:"Child2"
      }
 },
 sections[1]={
      title:"section1",
      children[0]={
           title:"Child0"
      },
      children[1]={
           title:"Child1"
      }
 }
 };
tabs[1]={
 sections[0]={
      title:"section0",
      children[0]={
           title:"Child0"
      },
      children[1]={
           title:"Child1"
      }
 },
 sections[1]={
      title:"section1",
      children[0]={
           title:"Child0"
      },
      children[1]={
           title:"Child1"
      }
 },
 sections[2]={
      title:"section2",
      children[0]={
           title:"Child0"
      },
      children[1]={
           title:"Child1"
      }
 }

}; };

Here is my code but I'm getting an "Unexpected Token" error at the first for loop within the tab object. 这是我的代码,但是在Tab对象内的第一个for循环中出现“意外令牌”错误。 Is this not allowed? 这是不允许的吗? How could I read these arrays and create objects like those above dynamically? 我如何读取这些数组并动态创建类似于上述对象的对象? The arrays (and subsequently the objects) can and will change as the .csv files change, which is why I need to dynamically create these objects. 数组(以及随后的对象)可以并且将随着.csv文件的更改而更改,这就是为什么我需要动态创建这些对象的原因。 These objects will be used in with AngularJS's ng-repeat to create the tabbed and side navigation for an app. 这些对象将与AngularJS的ng-repeat一起使用,以创建应用程序的选项卡式导航和侧面导航。

 this.tabData = tabsService.tabData;
    var tabCount = tabsService.tabData.length;
    var tabs={};
    var tCounter = 0;
    for (tCounter; tCounter<tabCount; tCounter++){
    var tabURL = "Contents/Product Groups/"+this.tabData[tCounter]+"/sectionOrder.csv";

    tabs[tCounter] ={
        "TabSectionData" : $.getCsvArray(tabs[tCounter].tabURL), //gets array from csv file
        "sectionCount" : TabSectionData.length

            for (sCounter = 0; sCounter<tabs[tCounter].sectionCount; sCounter++){
            "tabChildURL" : "Contents/Product Groups/"+this.tabData[tCounter]+"/"+tabs[tCounter].TabSectionData[sCounter]+"/order.csv", 
            "TabChildData" : $.getCsvArray(tabChildURL) //gets array from csv file

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

                        }
                    }
                }
            }


        }
    }

You can run a loop inside a function and instantly execute the function. 您可以在函数内部运行循环并立即执行该函数。 I created a Snippet to exemplify. 我创建了一个片段作为示例。

 var obj = { name: 'My Object', sections: (function () { var result = []; for (var i = 0; i < 10; i++) { var it = {}; result[i] = it; it.title = 'Hello'; it.children = []; for (var j = 0; j < i; j++) { it.children[j] = 'children' + j; } } return result; })() }; var json = JSON.stringify(obj, null, 4); jQuery('pre').append(json); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <pre></pre> 

Regarding your question, 'Can you use for loop inside an object [literal] ', perhaps to create a list of properties, you may do something like this: 关于您的问题,“您是否可以在对象[literal]中使用for循环”,也许要创建属性列表,您可以执行以下操作:

Enclose your for loop inside an immediately invoked function. for循环包含在立即调用的函数内。 From that function return the result of the loop. 从该函数return循环结果。

For example: 例如:

I want to dynamically add an array of properties using for loop. 我想使用for循环动态添加属性数组。

var items = {

    list : (function(){    // immediately invoked function

            var arr = []; // array

            for (var i = 0; i < 4; i++)
            {
                arr.push('item no. '+i); // add each item to array
            }

            return arr;   // return the array as value of the list property          

        })()
}

RESULT: 结果:
items = { list : [ "item no. 0", "item no. 1", "item no. 2", "item no. 3" ] } items = {列表:[“ 0号商品”,“ 1号商品”,“ 2号商品”,“ 3号商品”]}

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

相关问题 如何循环遍历 Javascript 对象数组,将对象指定为其中其他对象的子对象? 树/层次结构 - How can I loop through a Javascript object array assigning objects as children of other objects within? Tree/hierarchical structure 我如何破坏一个包含其他对象的 javascript 对象 - How can i destucture a javascript object containing other objects within it 您能解释一下JSON对象如何存储在Javascript对象中吗? - Can you explain how JSON objects are stored inside a Javascript object? 需要在对象内重复这些对象。 我可以在ng-repeat中使用$ index来实现这一点吗? - Need to repeat these objects within objects. Can I use $index with ng-repeat to make this happen? 如何将对象动态添加到对象内部的对象 - How to dynamically add objects to objects inside of an object Javascript动态创建对象内的对象 - Javascript Create Object inside Objects dynamically 在 Javascript 的运行时(动态)在 Object 中创建对象 - Create Objects inside an Object at Runtime(Dynamically) in Javascript 你可以在javascript中使用自定义对象作为对象的属性吗? - Can you use custom objects as properties of an object in javascript? 动态为javascript对象生成密钥时,gulp中断。 - gulp breaks when dynamically generating key for javascript objects. 遍历对象并获取其他对象(javascript) - Loop through object and get other objects (javascript)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM