Javascript for循环NESTED变量命名

时间:2017-03-13 22:42:45

标签: javascript loops variables

我需要修改一些旧的过滤代码。这是原始的(这里显示3个但重复20次:

if(lot.sequenceMap.length > 1){
    lotdata[lot.instanceName].plan1 = lot.sequenceMap[1].planCode;
}
if(lot.sequenceMap.length > 2){
    lotdata[lot.instanceName].plan2 = lot.sequenceMap[2].planCode;
}
if(lot.sequenceMap.length > 3){
    lotdata[lot.instanceName].plan3 = lot.sequenceMap[3].planCode;
}

显然我想为此使用for循环,但语法不允许它:

for(count = 1; count < 20; count++){
    if(lot.sequenceMap.length > count){
        lotdata[lot.instanceName].window['plan'+count] = lot.sequenceMap[count].planCode;
    }
}

我已经尝试了window['plan'+count],但这不起作用......有什么建议吗?

1 个答案:

答案 0 :(得分:1)

你很亲密! .window无效:

for(count = 1; count < 20; count++){
    if(lot.sequenceMap.length > count){
        lotdata[lot.instanceName]['plan'+count] = lot.sequenceMap[count].planCode;
    }
}