Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/macos/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 角度2中的数组索引超出范围_Javascript_Html_Arrays_Angular_Typescript - Fatal编程技术网

Javascript 角度2中的数组索引超出范围

Javascript 角度2中的数组索引超出范围,javascript,html,arrays,angular,typescript,Javascript,Html,Arrays,Angular,Typescript,我想声明一个4维数组,然后使用for()-循环一些东西,然后程序中断。这是我的密码: 打字稿: MoarInfo: any = [[[[]]]]; JavaScript: constructor(){ for(var i = 0; i < this.AllDataInfo[this.KontoAktuellYearIndex][this.KontoAktuellMonth].length; i++){ for(var a = 0; a < this.AllDataInfo[t

我想声明一个4维数组,然后使用
for()
-循环一些东西,然后程序中断。这是我的密码:

打字稿:

MoarInfo: any = [[[[]]]];
JavaScript:

constructor(){
 for(var i = 0; i < this.AllDataInfo[this.KontoAktuellYearIndex][this.KontoAktuellMonth].length; i++){
  for(var a = 0; a < this.AllDataInfo[this.KontoAktuellYearIndex][this.KontoAktuellMonth][i].length; a++){
    for(var b = 0; b < this.AllDataInfo[this.KontoAktuellYearIndex][this.KontoAktuellMonth][i][a].length; b++){

      this.MoarInfo[i][a][b][0] = this.AllDataInfo[this.KontoAktuellYearIndex][this.KontoAktuellMonth][i][a][b][0];
      this.MoarInfo[i][a][b][1] = this.AllDataInfo[this.KontoAktuellYearIndex][this.KontoAktuellMonth][i][a][b][1];
      this.MoarInfo[i][a][b][2] = 'DetailsSpan';

    }
  }
}
}
在JavaScript中,我尝试声明一个新数组,然后在
MoarInfo
数组上推送一些元素,使用不同的函数(split、unshift、push、concat),但没有任何效果


我做错了什么?

检查
这个.MoarInfo[I][a][b]
的大小。您正在尝试通过索引0,1,2获取值。看起来它的大小小于2,这是导致此错误的原因

      if(this.MoarInfo[i][a][b].size > 0){
         this.MoarInfo[i][a][b][0] = this.AllDataInfo[this.KontoAktuellYearIndex][this.KontoAktuellMonth][i][a][b][0];
      }
       if(this.MoarInfo[i][a][b].size > 1){
           this.MoarInfo[i][a][b][1] = this.AllDataInfo[this.KontoAktuellYearIndex][this.KontoAktuellMonth][i][a][b][1];
       }
       if(this.MoarInfo[i][a][b].size > 2){
           this.MoarInfo[i][a][b][2] = 'DetailsSpan';
       }

好吧,我自己想出来的。您必须将
数组中
维度的
元素设置为空,然后才能用内容填充它们。首先,我在
TypeScript
中声明了一个
Array
,如下
AnArray=[]。然后我切换到
JavaScript
(切换到
constructor()
函数)并用空白的
元素填充它。我用
this.AnArray.push()归档了它。如果要为第一个
维度设置
元素
,请使用
推送([])
,如果您想要第四个
维度的
元素
,请使用
推送([[]])。您可以像这样设置内容空间
push([['E1',0,0,'E2']])。现在您可以使用以下语法:

alert( this.AnArray[0][0][0][3] ); //returns 'E2'
我的项目中的完整代码现在运行良好,如下所示:

 for(var i = 0; i < this.AllDataInfo[this.KontoAktuellYearIndex][this.KontoAktuellMonth].length; i++){
  this.test.push([[[]]]);

  for(var a = 0; a < this.AllDataInfo[this.KontoAktuellYearIndex][this.KontoAktuellMonth][i].length; a++){
    this.test[i].push([[]]);

    for(var b = 0; b < this.AllDataInfo[this.KontoAktuellYearIndex][this.KontoAktuellMonth][i][a].length; b++){
      this.test[i][a].push(['',0,'']);

      this.test[i][a][b][0] = this.AllDataInfo[this.KontoAktuellYearIndex][this.KontoAktuellMonth][i][a][b][0];
      this.test[i][a][b][1] = this.AllDataInfo[this.KontoAktuellYearIndex][this.KontoAktuellMonth][i][a][b][1];
      this.test[i][a][b][2] = 'DetailsSpan';

    }
  }
}
for(var i=0;i
我想知道是否有比使用数组更好的方法,但如果您也需要,可以这样做

  • 干杯

我猜是用
这个.MoarInfo[I][a][b]。长度
?在本例中为0
this.MoarInfo[i][a]。长度
为1,
this.MoarInfo[i]。长度
也为1。是的,你是对的!然后最好在访问sit之前检查索引。用同样的方法更新了答案。谢谢你的答案,但是这避免了错误,如果数组能保存我告诉他的值,我将不胜感激^^
 for(var i = 0; i < this.AllDataInfo[this.KontoAktuellYearIndex][this.KontoAktuellMonth].length; i++){
  this.test.push([[[]]]);

  for(var a = 0; a < this.AllDataInfo[this.KontoAktuellYearIndex][this.KontoAktuellMonth][i].length; a++){
    this.test[i].push([[]]);

    for(var b = 0; b < this.AllDataInfo[this.KontoAktuellYearIndex][this.KontoAktuellMonth][i][a].length; b++){
      this.test[i][a].push(['',0,'']);

      this.test[i][a][b][0] = this.AllDataInfo[this.KontoAktuellYearIndex][this.KontoAktuellMonth][i][a][b][0];
      this.test[i][a][b][1] = this.AllDataInfo[this.KontoAktuellYearIndex][this.KontoAktuellMonth][i][a][b][1];
      this.test[i][a][b][2] = 'DetailsSpan';

    }
  }
}