如何在其他对象中动态生成javascript对象.你能在对象内部使用for循环吗
How to dynamically generate javascript objects within other objects. Can you use a for loop inside an object?
我希望实现一组类似于以下的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"]}
相关文章:
- 访问JSON对象内部的数组元素
- 如何确定对象内部包含对象的JSON的长度
- 正在更新mongod中两个对象内部的数组
- 如何访问对象内部的“categoryIds”字段/键,该对象包含mongodb's `ObjectId(s)`数
- 在javascript中移动对象内部的元素
- 基于使用angular在下拉列表中选择的对象,迭代对象内部的对象
- 如何清除Javascript对象内部的setTimeout
- 为什么数组在对象内部迭代对象时存储重复值
- 我如何使用D3来强制用户点击g.node对象内部的圆形对象
- 正在获取对象内部数组中的对象总数
- 从对象内部调用knockout.js ko.applyBindings()
- 使用ng-src增强对象内部的数组
- 如何在javascript中从对象内部的数组中获取数据
- 对象内部函数内的对象文本的范围
- 在 Forerunner 数据库集合中创建主键时,key 属性是否可以位于集合对象的对象内部
- 如何过滤对象内部深度堆叠的数据(并在之后编辑删除它)
- 对象内部数组中的角度 ng 重复键
- 访问对象内部的数组
- Jhon Papa Angular Style guide Factory-对象内部的函数未运行
- 检查数组中的项是否存在于对象内部