Js:嵌套中的多维数组错误,而其中一半仍然有效

时间:2016-11-14 23:22:06

标签: javascript arrays multidimensional-array nested

任何人都可以解释为什么第4个子阵列不再工作了吗?我相信因为input[(i + 1)]undefined?但它适用于另一个...... 我是新手,还在学习如何找出最佳选择。

function dataHandling(){


    for (var i=0;i < input.length   ; i++){
    for(var j=0; j < input[i].length; j++)
     /*
      if(j === 0){
         console.log("Nomor ID: "+ input[i][j] );
      }
      else if(j=== 1){
        console.log("Name: "+ input[i][j] );
      }
      else if(j=== 2){
        console.log("Birthplace n date: "+ input[i][j] +" " + input[i+1][j+1]);
      }
      else if(j=== 4){
        console.log("Hobby: "+ input[i][j] +"\n" );
      }
      */
     switch(j){
        case 0:
          console.log("Nomor ID: "+ input[i][j] );
          break;

          case 1:
          console.log("Name: "+ input[i][j] );
          break;

          case 2:
          console.log("Birthplace and date: "+ input[i][j] +" " + input[i+1][j+1]);
          break;

          case 3:
         // console.log("birthdate: "+ input[i][j] );
          break;

          case 4:
          console.log("Hobby: "+ input[i][j] +"\n" );
          break;

           default:
        break;
      }


    }

}

 var input = [
                ["0001", "Roman Alamsyah", "Bandar Lampung", "21/05/1989", "Reading"],
                ["0002", "Dika Sembiring", "Medan", "10/10/1992", "Playing Guitar"],
                ["0003", "Winona", "Ambon", "25/12/1965", "Cooking"],
                ["0004", "Bintang Senjaya", "Martapura", "6/4/1970", "Codding"]
              ];

dataHandling(input);

虽然它适用于第1至第3个阵列,但它总是在第4个阵列中出现错误:

Nomor ID: 0003
Name: Winona
Birthplace n date: Ambon 6/4/1970
Hobby: Cooking

Nomor ID: 0004
Name: Bintang Senjaya
TypeError: input[(i + 1)] is undefined <<< 

我可以理解,因为第一个i会出错,但只有i的第4个无法读取下一个子数组。 (很抱歉以新手的方式解释,仍然很难用有限的知识解释。)

1 个答案:

答案 0 :(得分:2)

首先,我想说你到目前为止通过学习Javascript做得很好。您在i=4使用input[i+1][j+1]时尝试访问第五个数组时出错。幸运的是,这不是一个问题;你想要做的是访问相同的子数组,但是下一个项目,所以只有j应该增加1(input[i][j+1]):

&#13;
&#13;
function dataHandling(){


    for (var i=0;i < input.length   ; i++){
    for(var j=0; j < input[i].length; j++)
     /*
      if(j === 0){
         console.log("Nomor ID: "+ input[i][j] );
      }
      else if(j=== 1){
        console.log("Name: "+ input[i][j] );
      }
      else if(j=== 2){
        console.log("Birthplace n date: "+ input[i][j] +" " + input[i+1][j+1]);
      }
      else if(j=== 4){
        console.log("Hobby: "+ input[i][j] +"\n" );
      }
      */
     switch(j){
        case 0:
          console.log("Nomor ID: "+ input[i][j] );
          break;

          case 1:
          console.log("Name: "+ input[i][j] );
          break;

          case 2:
          console.log("Birthplace and date: "+ input[i][j] +" " + input[i][j+1]);
          break;

          case 3:
         // console.log("birthdate: "+ input[i][j] );
          break;

          case 4:
          console.log("Hobby: "+ input[i][j] +"\n" );
          break;

           default:
        break;
      }


    }

}

 var input = [
                ["0001", "Roman Alamsyah", "Bandar Lampung", "21/05/1989", "Reading"],
                ["0002", "Dika Sembiring", "Medan", "10/10/1992", "Playing Guitar"],
                ["0003", "Winona", "Ambon", "25/12/1965", "Cooking"],
                ["0004", "Bintang Senjaya", "Martapura", "6/4/1970", "Codding"]
              ];

dataHandling(input);
&#13;
&#13;
&#13;