我有4个对象嵌套在一起。我试图在所有记录的最内层对象中更改“chk”属性的值。每个对象都有可变数量的属性。
基本结构是这样的
EMPLOYEE NAME
JOB#
PHASE
CODE
DAYSoFwEEK
CHK
我试图用in循环来实现这个目标
this.chkEmp = function(e)
{
/*e is an event listener which is triggered by a click on one of the check boxes. the id is the value of the outer most object*/
var i = e.target.id;
/*loop through jobs
this is where I get in trouble j is the job which is an object
so I think "this.recs[i][j]" is not a proper reference
*/
for ( var j in this.recs[i] )
{
/*loop through phase codes*/
for ( var p in this.recs[i][j] )
{
for ( var c in this.recs[i][j][p] )
{
this.recs[i][j][p][c].chk = e.target.checked;
}
}
}
};
如何遍历所有数据并将chk的值从true更改为false?
以下是一些示例数据
"Last_Name, First_Name" Object { 85109={...}, 85665={...}, 85762={...}}
85109 Object { 60={...}}
85665 Object { 60={...}}
60 Object { 1={...}, 3={...}}
1 Object { 0=0, 1=0, 2=0, more...}
0 0
1 0
2 0
3 2.75
4 0
5 0
6 0
chk true
3 Object { 0=0, 1=0, 2=0, more...}
85762 Object { 60={...}}
60 Object { H={...}}
H Object { 0=0, 1=10.5, 2=10.75, more...}
0 0
1 10.5
2 10.75
3 5.75
4 0
5 0
6 0
chk true
答案 0 :(得分:1)
重写如下(使用更有意义的变量名称)应该使程序更容易推理。
this.chkEmp = function (e) {
/*e is an event listener which is triggered by a click on one of the check boxes. the id is the value of the outer most object*/
var i = e.target.id;
/*loop through jobs
this is where I get in trouble j is the job which is an object
so I think "this.recs[i][j]" is not a proper reference
*/
var recs = this.recs[i];
var recsJ, recsJp, recsJpc;
for (var j in recs) {
recsJ = recs[j];
/*loop through phase codes*/
for (var p in recsJ) {
recsJp = recsJ[p];
for (var c in recsJp) {
recsJpc = recsJp[c];
recsJpc.chk = e.target.checked;
}
}
}
};
答案 1 :(得分:0)
现在我终于理解为什么我的原始代码不起作用了。除了更新嵌套对象,我还想关闭一些相关的复选框。
在下面的代码中 变量"工作","阶段"和#34;代码"是对象的引用
而" j"," p"," c"是对象的名称。
所以我真的需要两个,一个是达到文档控件的名称(复选框),另一个是引用嵌套对象,所以我可以循环它。
this.chkEmp = function(e)
{
var i = e.target.id;
var recs = this.recs[i];
var jobs, phase, code;
for (var j in recs) {
jobs = recs[j];
/*loop through phase codes*/
for (var p in jobs) {
phase = jobs[p];
for (var c in phase) {
code = phase[c];
code.chk = e.target.checked;
if (document.getElementById(i+"~"+j+'~'+p+'~'+c))
{
document.getElementById(i+"~"+j+'~'+p+'~'+c).checked = e.target.checked;
}
}
}
}
};