How to create an instance and get the length of a multidimensional array / matrix?

How to create a multidimensional array / matrix in JavaScript and Typescript, and how to get the matrix length (number of rows) and matrix row lengths?

+5
source share
1 answer

In Typescript, you will create a Matrix / Multimimensional 2x6 array using this syntax:

var matrix: number[][] = [ [ -1, 1, 2, -2, -3, 0 ], [ 0.1, 0.5, 0, 0, 0, 0 ] ]; //OR var matrix: Array<number>[] = [ [ -1, 1, 2, -2, -3, 0 ], [ 0.1, 0.5, 0, 0, 0, 0 ] ]; 

The equivalent in JavaScript will look like this:

 var matrix = [ [ -1, 1, 2, -2, -3, 0 ], [ 0.1, 0.5, 0, 0, 0, 0 ] ]; 

JavaScript syntax is also valid in TypeScript, since types are optional, but Typescript syntax is not valid in JavaScript.

To get different lengths, you have to use the same syntax in JS and TS:

 var matrixLength = matrix.length; // There are two dimensions, so the length is 2. This is also the column size. var firstDimensionLength = matrix[0].length; // The first dimension row has 6 elements, so the length is 6 var secondDimensionLength = matrix[1].length; // The second dimension row has 6 elements so the length is 6 
+6
source

Source: https://habr.com/ru/post/1203137/


All Articles