多维数组声明问题javascript

时间:2014-04-28 19:00:22

标签: javascript jquery multidimensional-array

我有两个多维数组$ path和$ record我必须循环并更新数组值有一些数组声明的问题我无法找到。

控制台错误:未捕获的TypeError:无法读取属性' 0'在行if($record[$path[a][icell]][j]){

中未定义的

$ record之前已分配了值$record[i][2] = true;与$ path相同,但是在发生行错误$path = [[$start]];之前从数组中仅重新分配了5个值,所以为什么当两个数组结合使用if($record[$path[a][icell]][j]){时会发生错误。

如果你需要jsfiddle,我会提供,但我想它并没有服务于包含许多其他无关脚本的目的,所以不必要地使代码压倒性的。 希望我尽可能清楚地表明自己需要明确提供

JQUERY:

var $record = new Array();
  $(".solve button").on("click",function(){
    if(($start+1)&&($end+1)){    
      var i=0,
          x,y;

      $("td").each(function(){
          if(!$.isArray($record[i])) { $record[i] = []; }
              if(!$(this).hasClass("top")){
                  if($(this).css("border-top-color") == "rgb(0, 0, 0)"){
                    $record[i][0] = true;
                  }
              } else { $record[i][0] = false; }
              if(!$(this).hasClass("right")){
                  if($(this).css("border-right-color") == "rgb(0, 0, 0)"){
                      $record[i][1] = true;
                  }
              } else { $record[i][1] = false; }
              if(!$(this).hasClass("bottom")){
                  if($(this).css("border-bottom-color") == "rgb(0, 0, 0)"){
                      $record[i][2] = true;
                  }
              } else { $record[i][2] = false; }
              if(!$(this).hasClass("left")){
                  if($(this).css("border-left-color") == "rgb(0, 0, 0)"){
                      $record[i][3] = true;
                  }
              } else { $record[i][3] = false; }
              i++;
          });
          //console.log("record[2][0] : "+$record[2][0]);    
          } else {
              $("#both").fadeIn("fast",function(){
                  $("#both").delay(2000).fadeOut();
              });
          }

          /* finding paths */

          var $path = new Array(),  // $path[path_index][cell_index]
              ipath=0,icell=0,
              fresh=old=0;
              $path = {};
          if(!$.isArray($path[0])) { $path[0] = []; }
          $path = [[$start]];
          for(i=0;i<$("#maze td").length;i++){      
              for(j=0;j<4;j++){
                    for(a=0;a<=ipath;a++){
                        icell = $path[a][$path[a].length-1];
                        if(icell+1){
                            if($record[$path[a][icell]][j]){
                            old=fresh;
                            if(!$.isArray($path[a])) { $path[a] = []; }
                            if(j=0){
                                for(k=0;k<=icell;k++){
                                    $path[a][k] = $path[a-1][k];
                                }
                                $path[a][icell+1] = $path[a][icell] - $cols; // moving             top
                                ipath++;
                                } else if (j=1){
                                    for(k=0;k<=icell;k++){
                                        $path[a][k] = $path[a-1][k];
                                    }
                                    $path[a][icell+1] = $path[a][icell] + 1;  // moving                     right
                                    ipath++;
                                } else if (j=2){
                                    for(k=0;k<=icell;k++){
                                        $path[a][k] = $path[a-1][k];
                                    }
                                    $path[a][icell+1] = $path[a][icell] - $cols; // moving bottom                           
                                    ipath++;
                                } else {
                                    for(k=0;k<=icell;k++){
                                        $path[a][k] = $path[a-1][k];
                                    }
                                    $path[a][icell+1] = $path[a][icell] - 1;  // moving                     left
                                    ipath++;
                                }
                            }
                        }
                    }
                }
            }
      });

0 个答案:

没有答案