Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

angular2 array of boolean

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;
like image 214
K Oul Avatar asked Nov 30 '25 05:11

K Oul


1 Answers

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

like image 131
Dawid Zbiński Avatar answered Dec 02 '25 19:12

Dawid Zbiński



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!