@technobuddha > library > Array > Construction
Function: create2dArray()
ts
function create2dArray<T>(
width: number,
height: number,
fill: T | (this: void, x: number, y: number) => T): T[][];Defined in: create2d-array.ts:22
Create a two dimensional array with all elements initialized
Type Parameters
| Type Parameter | Description |
|---|---|
T | Type of the elements in the array |
Parameters
| Parameter | Type | Description |
|---|---|---|
width | number | Width of the array |
height | number | Height of the array |
fill | T | (this: void, x: number, y: number) => T | Value to fill the array, or a function returning the fill value for each element |
Returns
T[][]
Remarks
Array is accessed by
js
array[w][h]Example
typescript
create2DArray(2, 3, 0); // [[0, 0, 0], [0, 0, 0]]
create2DArray(2, 3, (x, y) => x + y); // [[0, 1, 2], [1, 2, 3]]