如何在其他对象中动态生成javascript对象.你能在对象内部使用for循环吗

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

本文关键字:对象 内部 for 循环 其他 动态 javascript      更新时间:2023-09-26

我希望实现一组类似于以下的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"
      }
 }

};

这是我的代码,但在选项卡对象中的第一个for循环中,我收到了一个"意外令牌"错误。这是不允许的吗?我如何读取这些数组并动态地创建像上面那样的对象?数组(以及随后的对象)可以并且将随着.csv文件的更改而更改,这就是我需要动态创建这些对象的原因。这些对象将与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]
                        }
                    }
                }
            }

        }
    }

您可以在函数内部运行循环并立即执行该函数。我创建了一个Snippet来举例说明。

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>

关于你的问题,"你能在对象内部使用for循环吗[literal]",也许为了创建一个属性列表,你可以做这样的事情:

for循环封装在立即调用的函数中。从该函数return得到循环的结果。

例如:

我想使用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          
        })()
}

结果:
项目={列表:["项目编号0","第1项","第2项","项目3"]}