Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 嵌套循环以动态创建对象_Javascript_Loops - Fatal编程技术网

Javascript 嵌套循环以动态创建对象

Javascript 嵌套循环以动态创建对象,javascript,loops,Javascript,Loops,我正在尝试创建一个循环或嵌套循环,该循环将创建一(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]; ob

我正在尝试创建一个循环或嵌套循环,该循环将创建一(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];
};
//创建所有对象,distAttr长度为19
对于(变量i=0;i
您可以在单个循环中构建对象:

// Set up some variables and the field values you will use:
var j,
    obj,
    ec = municipo[0],
    ed = municipo[1],
    go = municipo[2],
    he = municipo[3],
    in = municipo[4],
    so = municipo[5];

// Loop through the array.
for (i = 0; i < distAttr.length; i++) {
    // Create an object with a country field. 
    obj = { country: distAttr[i] };
    // Populate the other fields.
    obj[ec] = econ[i];
    obj[ed] = edu[i];
    obj[go] = gov[i];
    obj[he] = health[i];
    obj[in] = infra[i];
    obj[so] = social[i];
    // Set the array index to contain the object
    // (and if you need it then create a global object `objx`
    //  - not sure if you need it though.)
    chartArray[i] = window['obj'+i] = obj;
};
//设置一些变量和要使用的字段值:
var j,
obj,
ec=市政[0],
ed=市政[1],
go=市政[2],
他=市政[3],
in=市政[4],
so=市政[5];
//在数组中循环。
对于(i=0;i
但是您面临的问题是什么?我在这里没有看到嵌套循环。我不知道如何构建与上述示例具有相同结构的多个对象。窗口['obj'+i][municipo[j]]=econ[i];不创建对象。
obj
将在每次迭代时引用一个新对象,但在迭代结束时
obj
被指定为
chartArray
的一个元素,因此该数组中维护的引用不会丢失或覆盖。您将得到一个包含多个不同对象的数组(而不是多次覆盖该对象)。将
obj
声明为循环范围之外的变量的原因是JavaScript不会在每次迭代中尝试创建新变量(并分配新内存)。我不确定,但我认为引擎创建了一个新变量,但杀死了旧变量。它将在每次迭代中创建一个
{}
对象,但不需要在每次迭代中重新创建
obj
(作为对
{}
的引用),也不需要垃圾收集
obj
(因为它将在后续迭代中重新使用)。如果在循环中声明
var obj
,则它可能需要在每次迭代中同时创建这两个变量,还可能需要对每次迭代的
obj
引用进行垃圾收集(取决于它如何优化块)。