我试图创建一个循环或嵌套的循环,这将创建一个(1)包含许多对象数组:嵌套循环动态创建对象
// example of the object structure
obj0.country = distAttr[0];
obj0[municipo[0]] = econ[0];
obj0[municipo[1]] = edu[0];
obj0[municipo[2]] = gov[0];
obj0[municipo[3]] = health[0];
obj0[municipo[4]] = infra[0];
obj0[municipo[5]] = social[0];
obj1.country = distAttr[1];
obj1[municipo[0]] = econ[1];
obj1[municipo[1]] = edu[1];
obj1[municipo[2]] = gov[1];
obj1[municipo[3]] = health[1];
obj1[municipo[4]] = infra[1];
obj1[municipo[5]] = social[1];
// ... obj18
这是我到目前为止有:
// create all the objects, distAttr length is 19
for (var i = 0; i < distAttr.length; i++) {
window['obj'+i ] = {};
};
// distName length is 6
var number = distName.length;
// this loop I can't figure out
for (var j = 0; i < distName.length; j++) {
window['obj'+i ][municipo[j]] = econ[i];
};
// bind the objects to the array
for (var i = 0; i < distAttr.length; i++) {
chartArray[i] = window['obj'+i];
};
但是,什么是你面临的问题? – khalid13
我在这里看不到嵌套循环。 – brouxhaha
我无法弄清楚如何建立与上面的例子具有相同结构的多个对象。窗口['obj'+ i] [municipo [j]] = econ [i];不会创建对象。 – Bwyss