错误:循环变量未定义

时间:2016-09-05 07:07:45

标签: javascript jquery for-loop undefined jsplumb

我试图循环一个数组,但它无法看到预期的输出。相反,当我尝试调试时,它表示变量r未定义。这是用于for loop的变量。

调试界面

Debugged

上下文代码

function checkClassType(idOfEl,dropElem)
{
    var ElementType="";
    if (dropElem == "stream ui-draggable") {

        for(var r=0;r<100;r++)
        {
            if(createdImportStreamArray[r][0]==idOfEl)
            {
                ElementType="importStream";
                finalArray[idOfEl-1][2]= createdImportStreamArray[r][1]; //Selected Stream from Predefined Streams
                finalArray[idOfEl-1][3]= createdImportStreamArray[r][2]; //asName

            }
            else if(createdExportStreamArray[r][0]==idOfEl)
            {
                ElementType="exportStream";
                finalArray[idOfEl-1][2]= createdExportStreamArray[r][1]; //Selected Stream from Predefined Streams
                finalArray[idOfEl-1][3]= createdExportStreamArray[r][2]; //asName
            }
            else 
            {
                ElementType="definedStream";
                finalArray[idOfEl-1][2]= createdDefinedStreamArray[r][1]; //Stream Name
                finalArray[idOfEl-1][3]= createdExportStreamArray[r][4]; //Number of Attributes
                finalArray[idOfEl-1][4]=[];
                for(var f=0;f<createdExportStreamArray[r][4];f++)
                {
                    finalArray[idOfEl-1][4][f][0]=createdDefinedStreamArray[r][2][f][0]; //Attribute Name
                    finalArray[idOfEl-1][4][f][1]=createdDefinedStreamArray[r][2][f][1]; // Attribute Type
                }
            }
        }
    }

    else if (dropElem == "wstream ui-draggable") {
             //Continues...

但是,根据我的假设,r将继续获取0到99之间的值。那么它如何可能未定义

1 个答案:

答案 0 :(得分:1)

尝试在方法的最开头声明r变量

function checkClassType(idOfEl,dropElem)
{
var r=0
var ElementType="";
if (dropElem == "stream ui-draggable") {

    for(;r<100;r++)
    {
        if(createdImportStreamArray[r][0]==idOfEl)
        {
            ElementType="importStream";
            finalArray[idOfEl-1][2]= createdImportStreamArray[r][1]; //Selected Stream from Predefined Streams
            finalArray[idOfEl-1][3]= createdImportStreamArray[r][2]; //asName

        }
        else if(createdExportStreamArray[r][0]==idOfEl)
        {
            ElementType="exportStream";
            finalArray[idOfEl-1][2]= createdExportStreamArray[r][1]; //Selected Stream from Predefined Streams
            finalArray[idOfEl-1][3]= createdExportStreamArray[r][2]; //asName
        }
        else 
        {
            ElementType="definedStream";
            finalArray[idOfEl-1][2]= createdDefinedStreamArray[r][1]; //Stream Name
            finalArray[idOfEl-1][3]= createdExportStreamArray[r][4]; //Number of Attributes
            finalArray[idOfEl-1][4]=[];
            for(var f=0;f<createdExportStreamArray[r][4];f++)
            {
                finalArray[idOfEl-1][4][f][0]=createdDefinedStreamArray[r][2][f][0]; //Attribute Name
                finalArray[idOfEl-1][4][f][1]=createdDefinedStreamArray[r][2][f][1]; // Attribute Type
            }
        }
    }
}

else if (dropElem == "wstream ui-draggable") {
         //Continues...