Javascript For循环中的未定义属性

时间:2018-05-16 12:05:44

标签: javascript scope

我无法在StackOverflow上看到这个问题的答案,但是我觉得它会让人感到尴尬,但是这里有。

var project_ID;
var projectName;

var tabulatorRowData = {};
var projectData = []
var mainColumnHeading = {
        "id": project_ID,
        "projectID": project_ID,
        "projectName": projectName
}
tabulatorRowData.projectData = projectData;
for (i=0; i < 10; i++) {
    mainColumnHeading.id = 1;
    mainColumnHeading.projectID = 2;
    mainColumnHeading.projectName = "project Name";
    tabulatorRowData.projectData.push(mainColumnHeading);
    for (j=0; j < 10; j++) {
        tabulatorRowData.projectData[j].hours = j;
        tabulatorRowData.projectData[j].progress = j+20;
    }
}
console.log(tabulatorRowData);
console.log(JSON.stringify(tabulatorRowData));

我正在使用Node.js库在Eclipse中测试并运行它,并返回以下错误;

tabulatorRowData.projectData[j].hours = j;
TypeError: Cannot set property 'hours' of undefined

现在,如果我将违规代码置于for (i=0; i < 10; i++) {内,那么我知道这是一个范围问题,但无法确定我可以做出什么声明来修复它。

3 个答案:

答案 0 :(得分:2)

在这种情况下,循环的嵌套是错误的,你需要将它们分开:

tabulatorRowData.projectData

原因是您要在i循环中向i添加项目,因此您无法一次访问所有10个项目,直到{{1}} - 循环完了。

答案 1 :(得分:1)

projectData初始化为空数组,内部for循环为每次主迭代运行10次。说,对于i=0的第一次迭代,tabulatorRowData.projectData[1]将是未定义的,依此类推。

您可以在主循环

之后移动内循环
for (i=0; i < 10; i++) {
    mainColumnHeading.id = 1;
    mainColumnHeading.projectID = 2;
    mainColumnHeading.projectName = "project Name";
    tabulatorRowData.projectData.push(mainColumnHeading);
}

for (j=0; j < 10; j++) {
    tabulatorRowData.projectData[j].hours = j;
    tabulatorRowData.projectData[j].progress = j+20;
}

注意tabulatorRowData.projectData中推送的项目为mainColumnHeading,并且对象的引用对于所有数组项都相同。这意味着在循环之后,最终结果将是指向同一对象的10个项目的数组。

答案 2 :(得分:0)

这个问题引起了很多讨论,可能反映了问题的不良框架;所以就上面的问题而言,答案是正确的,但是因为我没有提到那些回答无法解决真正问题的意图。所以虽然我会发布正确的实现来帮助其他人解决这个问题。

基本上我想要做的是创建一个具有多个name:value对的对象,这些对可以从数据库而不是静态声明生成,我想为每个返回的数据库行执行此操作

虽然问题中未说明,但事实证明,可以使用.表示法或square bracket表示法向对象添加属性,并且.表示法的范围有限。

无论如何,这是答案,我希望它有所帮助。

var tabulatorRowData = {};
var projectData = []
tabulatorRowData.projectData = projectData;
for (var j=0;j<10;j++){
    var mainColumnHeading = new Object();
    mainColumnHeading.id = j;
    mainColumnHeading.projectID = j*2;
    mainColumnHeading.projectName = "newName"+j;
    tabulatorRowData.projectData.push(mainColumnHeading); 
    for (var i=0; i < 10; i++) {
        var hours = "hours"+i;
        var progress = "progress"+i;
            tabulatorRowData.projectData[j][hours] = i;
            tabulatorRowData.projectData[j][progress] = 2*i
    }
}

console.log(tabulatorRowData);
console.log(JSON.stringify(tabulatorRowData));

输出如下:

{"projectData":[{"id":0,"projectID":0,"projectName":"newName0","hours0":0,"progress0":0,"hours1":1,"progress1":2,"hours2":2,"progress2":4,"hours3":3,"progress3":6,"hours4":4,"progress4":8},
{"id":1,"projectID":2,"projectName":"newName1","hours0":0,"progress0":0,"hours1":1,"progress1":2,"hours2":2,"progress2":4,"hours3":3,"progress3":6,"hours4":4,"progress4":8},
{"id":2,"projectID":4,"projectName":"newName2","hours0":0,"progress0":0,"hours1":1,"progress1":2,"hours2":2,"progress2":4,"hours3":3,"progress3":6,"hours4":4,"progress4":8},
{"id":3,"projectID":6,"projectName":"newName3","hours0":0,"progress0":0,"hours1":1,"progress1":2,"hours2":2,"progress2":4,"hours3":3,"progress3":6,"hours4":4,"progress4":8},
{"id":4,"projectID":8,"projectName":"newName4","hours0":0,"progress0":0,"hours1":1,"progress1":2,"hours2":2,"progress2":4,"hours3":3,"progress3":6,"hours4":4,"progress4":8}]}

Tabulator表所需的表格。