To create a Two Dimensional Array in TypeScript, you can initialize an array of arrays with specified dimensions.
In this example,
matrix
with dimensions 3x3.const matrix: number[][] = [];
for (let i = 0; i < 3; i++) {
matrix[i] = [];
for (let j = 0; j < 3; j++) {
matrix[i][j] = i + j;
}
}
console.log(matrix);
[ [ 0, 1, 2 ], [ 1, 2, 3 ], [ 2, 3, 4 ] ]
In this example,
matrix
with dimensions 2x2.const matrix: string[][] = [];
for (let i = 0; i < 2; i++) {
matrix[i] = [];
for (let j = 0; j < 2; j++) {
matrix[i][j] = `Row ${i + 1}, Column ${j + 1}`;
}
}
console.log(matrix);
[ [ 'Row 1, Column 1', 'Row 1, Column 2' ], [ 'Row 2, Column 1', 'Row 2, Column 2' ] ]
In this tutorial, we learned How to create a Two Dimensional Array in TypeScript language with well detailed examples.