Reputation: 245
When declaring this :
public isCollapsedDet : boolean[][];
public isCollapsedCyc : boolean[] ;
I got the following error message :
/nestedForm/src/app/app.component.ts (95,7): Type 'boolean' is not assignable to type 'boolean[][]'.
I just need to get array as the following :
isCollapsedCyc[0] = true;
isCollapsedCyc[1] = false;
//
isCollapsedDet[0, 0] = true;
isCollapsedDet[0, 1] = true;
isCollapsedDet[1, 0] = false;
isCollapsedDet[1, 1] = true;
Upvotes: 3
Views: 9769
Reputation: 8484
When you are accessing a property in any class and if you want to make it as a class member then don't forget to mention this
and as Dawid said you can't assign values by separating indexes with comma(,)
export class HelloWorld implements OnInit{
// Declaring the variable for binding with initial value
yourName: string = '';
public isCollapsedDet : boolean[][] = [[], []];
isCollapsedCyc : boolean[] = [];
ngOnInit() {
this.isCollapsedCyc[0] = true;
this.isCollapsedCyc[1] = false;
//
this.isCollapsedDet[0][0] = true;
this.isCollapsedDet[0][1] = true;
this.isCollapsedDet[1][0] = false;
this.isCollapsedDet[1][1] = true;
}
}
Upvotes: 1
Reputation: 10526
If you really only need the elements you mentioned, you could do:
let isCollapsedDet: boolean[][] = [[], []];
let isCollapsedCyc: boolean[] = [];
isCollapsedCyc[0] = true;
isCollapsedCyc[1] = false;
isCollapsedDet[0][0] = true;
isCollapsedDet[0][1] = true;
isCollapsedDet[1][0] = false;
isCollapsedDet[1][1] = true;
Or simply:
let isCollapsedDet: boolean[][] = [
[true, true], [false, true]
];
let isCollapsedCyc: boolean[] = [true, false];
which can be reduced further because the compiler will infer the types based on the initialization:
let isCollapsedDet = [
[true, true], [false, false]
];
let isCollapsedCyc = [true, false];
Upvotes: 1
Reputation: 5826
You cannot add values to an array by nesting them with comma.
Type boolean[][]
means that there will be an array of arrays of booleans, so something like for example:
[[true, false], [false, true]] // this is boolean[][] or Array<Array<boolean>>
if you want to set the value for it, you need to nest it as an ordinary array:
isCollapsedDet[0, 0] = true;
// error - comma has nothing to do there
isCollapsedDet[0][0] = true;
// success - element isCollapsedDet[0][0] in array isCollapsedDet[0] is true
More about arrays in TypeScript can be found here - and a bit more advanced types here
Some useful answers found here: Multidimensional array initialization
Other links: TypeScript Multidimensional Arrays
Upvotes: 3