Skip to main content

New Array

Functions

newArray

newArray<T>(defaultValue, size): T[]

Initializes an array with all of the elements containing the specified default value.

The provided default value will be copied with the deepCopy function before adding it to the new array. Thus, you can initialize an array of arrays, or an array of maps, and so on. (If the deepCopy function was not used, then all of the array elements would just be references to the same underlying data structure.)

For example:

const arrayWithZeroes = newArray(0, 10); // Has 10 elements of 0.
const arrayWithArrays = newArray([0], 20); // Has 20 elements of an array with a 0 in it.

Type parameters

Name
T

Parameters

NameType
defaultValueT
sizeint

Returns

T[]

Defined in

packages/isaacscript-common/src/functions/newArray.ts:20