Skip to content

@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 ParameterDescription
TType of the elements in the array

Parameters

ParameterTypeDescription
widthnumberWidth of the array
heightnumberHeight of the array
fillT | (this: void, x: number, y: number) => TValue 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]]