Array index out of bounds in Angular 2
up vote
0
down vote
favorite
I want to declare an Array with 4 dimensions, then loop some stuff with for()
- and then the program breaks. Here is my code:
Typescript:
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';
}
}
}
}
The Problem definitively lies at the MoarInfo
array. I tested my code without it, and it works fine. I tried the following possibilities for the Typescript array declaration as well:
Moarinfo: any; MoarInfo = ; MoarInfo = [[[]]]; MoarInfo: any = [[[]]];
And in JavaScript, I tried to declare a new Array, and then push some elements on the MoarInfo
array, with different functions (split, unshift, push, concat) and nothing worked.
What am I doing wrong?
javascript html arrays angular typescript
add a comment |
up vote
0
down vote
favorite
I want to declare an Array with 4 dimensions, then loop some stuff with for()
- and then the program breaks. Here is my code:
Typescript:
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';
}
}
}
}
The Problem definitively lies at the MoarInfo
array. I tested my code without it, and it works fine. I tried the following possibilities for the Typescript array declaration as well:
Moarinfo: any; MoarInfo = ; MoarInfo = [[[]]]; MoarInfo: any = [[[]]];
And in JavaScript, I tried to declare a new Array, and then push some elements on the MoarInfo
array, with different functions (split, unshift, push, concat) and nothing worked.
What am I doing wrong?
javascript html arrays angular typescript
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
I want to declare an Array with 4 dimensions, then loop some stuff with for()
- and then the program breaks. Here is my code:
Typescript:
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';
}
}
}
}
The Problem definitively lies at the MoarInfo
array. I tested my code without it, and it works fine. I tried the following possibilities for the Typescript array declaration as well:
Moarinfo: any; MoarInfo = ; MoarInfo = [[[]]]; MoarInfo: any = [[[]]];
And in JavaScript, I tried to declare a new Array, and then push some elements on the MoarInfo
array, with different functions (split, unshift, push, concat) and nothing worked.
What am I doing wrong?
javascript html arrays angular typescript
I want to declare an Array with 4 dimensions, then loop some stuff with for()
- and then the program breaks. Here is my code:
Typescript:
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';
}
}
}
}
The Problem definitively lies at the MoarInfo
array. I tested my code without it, and it works fine. I tried the following possibilities for the Typescript array declaration as well:
Moarinfo: any; MoarInfo = ; MoarInfo = [[[]]]; MoarInfo: any = [[[]]];
And in JavaScript, I tried to declare a new Array, and then push some elements on the MoarInfo
array, with different functions (split, unshift, push, concat) and nothing worked.
What am I doing wrong?
javascript html arrays angular typescript
javascript html arrays angular typescript
edited Nov 15 at 7:53
Jesse de Bruijne
2,46861327
2,46861327
asked Nov 15 at 7:00
Bush_Did_911
94
94
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
up vote
1
down vote
Check the size of this.MoarInfo[i][a][b]
. You are trying to get the value by index 0,1,2. Looks like its size is less than 2 which is causing this error.
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';
}
I guess withthis.MoarInfo[i][a][b].length
? In this case its 0.this.MoarInfo[i][a].length
is 1 andthis.MoarInfo[i].length
is 1 aswell. And yeah, you are right!
– Bush_Did_911
Nov 15 at 7:14
Then better would be to have check for index before you access sit. Updated the answer with the same.
– Sunil Singh
Nov 15 at 7:17
Thank's for you answer man, but this avoids just the error, I would much appreciate it, if the Array would save the values I tell him to ^^
– Bush_Did_911
Nov 15 at 7:21
add a comment |
up vote
0
down vote
Okay, I figured it out for myself. You have to set the elements
of the dimensions
from the array
at first blank, then you can fill them with content. First I declared an Array
in TypeScript
like this AnArray = ;
. Then I switched to JavaScript
( to the constructer()
function ) and filled it with blank elements
. I archieved this with this.AnArray.push();
. If you want to set elements
for the first dimension
use push();
, if you want an element
for the 4th dimension
, use push([[]]);
. And you can set your content space like this push([[['E1',0,0,'E2']]]);
. Now you can use follwing syntax:
alert( this.AnArray[0][0][0][3] ); //returns 'E2'
The complete code from my project now works fine and looks like this:
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';
}
}
}
I wonder if there is a better way than using arrays, but if you want it too, you can do it like this.
- cheers
add a comment |
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
1
down vote
Check the size of this.MoarInfo[i][a][b]
. You are trying to get the value by index 0,1,2. Looks like its size is less than 2 which is causing this error.
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';
}
I guess withthis.MoarInfo[i][a][b].length
? In this case its 0.this.MoarInfo[i][a].length
is 1 andthis.MoarInfo[i].length
is 1 aswell. And yeah, you are right!
– Bush_Did_911
Nov 15 at 7:14
Then better would be to have check for index before you access sit. Updated the answer with the same.
– Sunil Singh
Nov 15 at 7:17
Thank's for you answer man, but this avoids just the error, I would much appreciate it, if the Array would save the values I tell him to ^^
– Bush_Did_911
Nov 15 at 7:21
add a comment |
up vote
1
down vote
Check the size of this.MoarInfo[i][a][b]
. You are trying to get the value by index 0,1,2. Looks like its size is less than 2 which is causing this error.
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';
}
I guess withthis.MoarInfo[i][a][b].length
? In this case its 0.this.MoarInfo[i][a].length
is 1 andthis.MoarInfo[i].length
is 1 aswell. And yeah, you are right!
– Bush_Did_911
Nov 15 at 7:14
Then better would be to have check for index before you access sit. Updated the answer with the same.
– Sunil Singh
Nov 15 at 7:17
Thank's for you answer man, but this avoids just the error, I would much appreciate it, if the Array would save the values I tell him to ^^
– Bush_Did_911
Nov 15 at 7:21
add a comment |
up vote
1
down vote
up vote
1
down vote
Check the size of this.MoarInfo[i][a][b]
. You are trying to get the value by index 0,1,2. Looks like its size is less than 2 which is causing this error.
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';
}
Check the size of this.MoarInfo[i][a][b]
. You are trying to get the value by index 0,1,2. Looks like its size is less than 2 which is causing this error.
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';
}
edited Nov 15 at 7:17
answered Nov 15 at 7:06
Sunil Singh
6,1122626
6,1122626
I guess withthis.MoarInfo[i][a][b].length
? In this case its 0.this.MoarInfo[i][a].length
is 1 andthis.MoarInfo[i].length
is 1 aswell. And yeah, you are right!
– Bush_Did_911
Nov 15 at 7:14
Then better would be to have check for index before you access sit. Updated the answer with the same.
– Sunil Singh
Nov 15 at 7:17
Thank's for you answer man, but this avoids just the error, I would much appreciate it, if the Array would save the values I tell him to ^^
– Bush_Did_911
Nov 15 at 7:21
add a comment |
I guess withthis.MoarInfo[i][a][b].length
? In this case its 0.this.MoarInfo[i][a].length
is 1 andthis.MoarInfo[i].length
is 1 aswell. And yeah, you are right!
– Bush_Did_911
Nov 15 at 7:14
Then better would be to have check for index before you access sit. Updated the answer with the same.
– Sunil Singh
Nov 15 at 7:17
Thank's for you answer man, but this avoids just the error, I would much appreciate it, if the Array would save the values I tell him to ^^
– Bush_Did_911
Nov 15 at 7:21
I guess with
this.MoarInfo[i][a][b].length
? In this case its 0. this.MoarInfo[i][a].length
is 1 and this.MoarInfo[i].length
is 1 aswell. And yeah, you are right!– Bush_Did_911
Nov 15 at 7:14
I guess with
this.MoarInfo[i][a][b].length
? In this case its 0. this.MoarInfo[i][a].length
is 1 and this.MoarInfo[i].length
is 1 aswell. And yeah, you are right!– Bush_Did_911
Nov 15 at 7:14
Then better would be to have check for index before you access sit. Updated the answer with the same.
– Sunil Singh
Nov 15 at 7:17
Then better would be to have check for index before you access sit. Updated the answer with the same.
– Sunil Singh
Nov 15 at 7:17
Thank's for you answer man, but this avoids just the error, I would much appreciate it, if the Array would save the values I tell him to ^^
– Bush_Did_911
Nov 15 at 7:21
Thank's for you answer man, but this avoids just the error, I would much appreciate it, if the Array would save the values I tell him to ^^
– Bush_Did_911
Nov 15 at 7:21
add a comment |
up vote
0
down vote
Okay, I figured it out for myself. You have to set the elements
of the dimensions
from the array
at first blank, then you can fill them with content. First I declared an Array
in TypeScript
like this AnArray = ;
. Then I switched to JavaScript
( to the constructer()
function ) and filled it with blank elements
. I archieved this with this.AnArray.push();
. If you want to set elements
for the first dimension
use push();
, if you want an element
for the 4th dimension
, use push([[]]);
. And you can set your content space like this push([[['E1',0,0,'E2']]]);
. Now you can use follwing syntax:
alert( this.AnArray[0][0][0][3] ); //returns 'E2'
The complete code from my project now works fine and looks like this:
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';
}
}
}
I wonder if there is a better way than using arrays, but if you want it too, you can do it like this.
- cheers
add a comment |
up vote
0
down vote
Okay, I figured it out for myself. You have to set the elements
of the dimensions
from the array
at first blank, then you can fill them with content. First I declared an Array
in TypeScript
like this AnArray = ;
. Then I switched to JavaScript
( to the constructer()
function ) and filled it with blank elements
. I archieved this with this.AnArray.push();
. If you want to set elements
for the first dimension
use push();
, if you want an element
for the 4th dimension
, use push([[]]);
. And you can set your content space like this push([[['E1',0,0,'E2']]]);
. Now you can use follwing syntax:
alert( this.AnArray[0][0][0][3] ); //returns 'E2'
The complete code from my project now works fine and looks like this:
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';
}
}
}
I wonder if there is a better way than using arrays, but if you want it too, you can do it like this.
- cheers
add a comment |
up vote
0
down vote
up vote
0
down vote
Okay, I figured it out for myself. You have to set the elements
of the dimensions
from the array
at first blank, then you can fill them with content. First I declared an Array
in TypeScript
like this AnArray = ;
. Then I switched to JavaScript
( to the constructer()
function ) and filled it with blank elements
. I archieved this with this.AnArray.push();
. If you want to set elements
for the first dimension
use push();
, if you want an element
for the 4th dimension
, use push([[]]);
. And you can set your content space like this push([[['E1',0,0,'E2']]]);
. Now you can use follwing syntax:
alert( this.AnArray[0][0][0][3] ); //returns 'E2'
The complete code from my project now works fine and looks like this:
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';
}
}
}
I wonder if there is a better way than using arrays, but if you want it too, you can do it like this.
- cheers
Okay, I figured it out for myself. You have to set the elements
of the dimensions
from the array
at first blank, then you can fill them with content. First I declared an Array
in TypeScript
like this AnArray = ;
. Then I switched to JavaScript
( to the constructer()
function ) and filled it with blank elements
. I archieved this with this.AnArray.push();
. If you want to set elements
for the first dimension
use push();
, if you want an element
for the 4th dimension
, use push([[]]);
. And you can set your content space like this push([[['E1',0,0,'E2']]]);
. Now you can use follwing syntax:
alert( this.AnArray[0][0][0][3] ); //returns 'E2'
The complete code from my project now works fine and looks like this:
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';
}
}
}
I wonder if there is a better way than using arrays, but if you want it too, you can do it like this.
- cheers
answered Nov 15 at 12:13
Bush_Did_911
94
94
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53314008%2farray-index-out-of-bounds-in-angular-2%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown