3

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;
2
  • i can only do isCollapsedDet[1][1] = true just set isCollapsedDet : boolean[] and each item isCollapsedDet[i] = [] Commented Jun 20, 2017 at 10:07
  • 1
    You might inspire yourself from answers to this question: stackoverflow.com/questions/38875051/… Commented Jun 20, 2017 at 10:10

3 Answers 3

3

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

Sign up to request clarification or add additional context in comments.

3 Comments

Thank you for you ALL, specially for the prompt answers. It's working now
because I use the isCollapsedDet in template, is this correct ? <span *ngIf="!isCollapsedDet[i][j]" class="glyphicon glyphicon-chevron-down"></span>
yes. probably, if the i and j variables are type of integer and the value exist in array - it should work
1

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];

Comments

1

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;
  }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.