[英]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.