javascript - js : multidimensional-array error in nested while half of this still working

标签 javascript arrays multidimensional-array nested

谁能解释一下为什么第四个子数组不再工作了?我相信是因为 input[(i + 1)]未定义?但它适用于另一个... 我是新手,仍在学习如何找出最佳选择。

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 无法读取下一个子数组。 (抱歉用新手的方式解释,知识有限还是很难解释。)

最佳答案

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

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);

关于javascript - js : multidimensional-array error in nested while half of this still working,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40599508/

相关文章:

javascript - Typescript:通用不可变集合

javascript - 在 Node 应用程序中使用第三方库

python - 计算每个指数平均值的最快方法

c# .net 覆盖多维数组的 ToString

mysql - 从多维数组获取值 | JSON

javascript - 学习 Javascript/Jquery Promise&Deferred, Issue with $.when 多次使用

javascript - 即使在添加 -webkit 后,CSS 动画也无法在 Safari Web 浏览器中运行

php - 根据键数组获取数组的子集

java - 如何在java中生成特定大小的二进制blob对象?

javascript - 在javascript中从多维数组或对象中删除元素