Skip to main content

Array

Functions

arrayEquals

arrayEquals<T>(array1, array2): boolean

Helper function for determining if two arrays contain the exact same elements. Note that this only performs a shallow comparison.

Type parameters

Name
T

Parameters

NameType
array1readonly T[]
array2readonly T[]

Returns

boolean

Defined in

packages/isaacscript-common/src/functions/array.ts:13


arrayRemove

arrayRemove<T>(originalArray, ...elementsToRemove): T[]

Builds a new array based on the original array without the specified element(s). Returns the new array. If the specified element(s) are not found in the array, it will simply return a shallow copy of the array.

This function is variadic, meaning that you can specify N arguments to remove N elements.

If there is more than one matching element in the array, this function will only remove the first matching element. If you want to remove all of the elements, use the arrayRemoveAll function instead.

Type parameters

Name
T

Parameters

NameType
originalArrayreadonly T[]
...elementsToRemovereadonly T[]

Returns

T[]

Defined in

packages/isaacscript-common/src/functions/array.ts:39


arrayRemoveAll

arrayRemoveAll<T>(originalArray, ...elementsToRemove): T[]

Shallow copies and removes the specified element(s) from the array. Returns the copied array. If the specified element(s) are not found in the array, it will simply return a shallow copy of the array.

This function is variadic, meaning that you can specify N arguments to remove N elements.

If there is more than one matching element in the array, this function will remove every matching element. If you want to only remove the first matching element, use the arrayRemove function instead.

Type parameters

Name
T

Parameters

NameType
originalArrayreadonly T[]
...elementsToRemovereadonly T[]

Returns

T[]

Defined in

packages/isaacscript-common/src/functions/array.ts:67


arrayRemoveAllInPlace

arrayRemoveAllInPlace<T>(array, ...elementsToRemove): boolean

Removes all of the specified element(s) from the array. If the specified element(s) are not found in the array, this function will do nothing.

This function is variadic, meaning that you can specify N arguments to remove N elements.

If there is more than one matching element in the array, this function will remove every matching element. If you want to only remove the first matching element, use the arrayRemoveInPlace function instead.

Type parameters

Name
T

Parameters

NameType
arrayT[]
...elementsToRemovereadonly T[]

Returns

boolean

True if one or more elements were removed, false otherwise.

Defined in

packages/isaacscript-common/src/functions/array.ts:88


arrayRemoveInPlace

arrayRemoveInPlace<T>(array, ...elementsToRemove): T[]

Removes the specified element(s) from the array. If the specified element(s) are not found in the array, this function will do nothing.

This function is variadic, meaning that you can specify N arguments to remove N elements.

If there is more than one matching element in the array, this function will only remove the first matching element. If you want to remove all of the elements, use the arrayRemoveAllInPlace function instead.

Type parameters

Name
T

Parameters

NameType
arrayT[]
...elementsToRemovereadonly T[]

Returns

T[]

The removed elements. This will be an empty array if no elements were removed.

Defined in

packages/isaacscript-common/src/functions/array.ts:121


arrayRemoveIndex

arrayRemoveIndex<T>(originalArray, ...indexesToRemove): T[]

Shallow copies and removes the elements at the specified indexes from the array. Returns the copied array. If the specified indexes are not found in the array, it will simply return a shallow copy of the array.

This function is variadic, meaning that you can specify N arguments to remove N elements.

Type parameters

Name
T

Parameters

NameType
originalArrayreadonly T[]
...indexesToRemovereadonly int[]

Returns

T[]

Defined in

packages/isaacscript-common/src/functions/array.ts:147


arrayRemoveIndexInPlace

arrayRemoveIndexInPlace<T>(array, ...indexesToRemove): T[]

Removes the elements at the specified indexes from the array. If the specified indexes are not found in the array, this function will do nothing.

This function is variadic, meaning that you can specify N arguments to remove N elements.

Type parameters

Name
T

Parameters

NameType
arrayT[]
...indexesToRemovereadonly int[]

Returns

T[]

The removed elements. This will be an empty array if no elements were removed.

Defined in

packages/isaacscript-common/src/functions/array.ts:172


arrayToString

arrayToString(array): string

Parameters

NameType
arrayreadonly unknown[]

Returns

string

Defined in

packages/isaacscript-common/src/functions/array.ts:198


combineArrays

combineArrays<T>(...arrays): T[]

Helper function to combine two or more arrays. Returns a new array that is the composition of all of the specified arrays.

This function is variadic, meaning that you can specify N arguments to combine N arrays. Note that this will only perform a shallow copy of the array elements.

Type parameters

Name
T

Parameters

NameType
...arraysreadonly readonly T[][]

Returns

T[]

Defined in

packages/isaacscript-common/src/functions/array.ts:216


copyArray

copyArray<T>(oldArray, numElements?): T[]

Helper function to perform a shallow copy.

Type parameters

Name
T

Parameters

NameTypeDescription
oldArrayreadonly T[]The array to copy.
numElements?intOptional. If specified, will only copy the first N elements. By default, the entire array will be copied.

Returns

T[]

Defined in

packages/isaacscript-common/src/functions/array.ts:235


emptyArray

emptyArray<T>(array): void

Helper function to remove all of the elements in an array in-place.

Type parameters

Name
T

Parameters

NameType
arrayT[]

Returns

void

Defined in

packages/isaacscript-common/src/functions/array.ts:253


filterMap

filterMap<OldT, NewT>(array, func): NewT[]

Helper function to perform a filter and a map at the same time. Similar to Array.map, provide a function that transforms a value, but return undefined if the value should be skipped. (Thus, this function cannot be used in situations where undefined can be a valid array element.)

This function is useful because the Array.map method will always produce an array with the same amount of elements as the original array.

This is named filterMap after the Rust function: https://doc.rust-lang.org/std/iter/struct.FilterMap.html

Type parameters

Name
OldT
NewT

Parameters

NameType
arrayreadonly OldT[]
func(element: OldT) => undefined | NewT

Returns

NewT[]

Defined in

packages/isaacscript-common/src/functions/array.ts:269


getArrayCombinations

getArrayCombinations<T>(array, includeEmptyArray, min?, max?): ReadonlyArray<readonly T[]>

Helper function to get all possible combinations of the given array. This includes the combination of an empty array.

For example, if this function is provided an array containing 1, 2, and 3, then it will return an array containing the following arrays:

  • [] (if includeEmptyArray is set to true)
  • [1]
  • [2]
  • [3]
  • [1, 2]
  • [1, 3]
  • [2, 3]
  • [1, 2, 3]

From: https://github.com/firstandthird/combinations/blob/master/index.js

Type parameters

Name
T

Parameters

NameTypeDescription
arrayreadonly T[]The array to get the combinations of.
includeEmptyArraybooleanWhether to include an empty array in the combinations.
min?intOptional. The minimum number of elements to include in each combination. Default is 1.
max?intOptional. The maximum number of elements to include in each combination. Default is the length of the array.

Returns

ReadonlyArray<readonly T[]>

Defined in

packages/isaacscript-common/src/functions/array.ts:309


getArrayDuplicateElements

getArrayDuplicateElements<T>(array): readonly T[]

Helper function to get the duplicate elements in an array. Only one element for each value will be returned. The elements will be sorted before they are returned.

Type parameters

NameType
Textends string | number

Parameters

NameType
arrayreadonly T[]

Returns

readonly T[]

Defined in

packages/isaacscript-common/src/functions/array.ts:362


getArrayIndexes

getArrayIndexes<T>(array): readonly int[]

Helper function to get an array containing the indexes of an array.

For example, an array of ["Apple", "Banana"] would return an array of [0, 1].

Note that normally, you would use the Object.keys method to get the indexes of an array, but due to implementation details of TypeScriptToLua, this results in an array of 1 through N (instead of an array of 0 through N -1).

Type parameters

Name
T

Parameters

NameType
arrayreadonly T[]

Returns

readonly int[]

Defined in

packages/isaacscript-common/src/functions/array.ts:388


getHighestArrayElement

getHighestArrayElement(array): number | undefined

Helper function to get the highest value in an array. Returns undefined if there were no elements in the array.

Parameters

NameType
arrayreadonly number[]

Returns

number | undefined

Defined in

packages/isaacscript-common/src/functions/array.ts:396


getLowestArrayElement

getLowestArrayElement(array): number | undefined

Helper function to get the lowest value in an array. Returns undefined if there were no elements in the array.

Parameters

NameType
arrayreadonly number[]

Returns

number | undefined

Defined in

packages/isaacscript-common/src/functions/array.ts:418


getRandomArrayElement

getRandomArrayElement<T>(array, seedOrRNG, exceptions?): T

Helper function to get a random element from the provided array.

If you want to get an unseeded element, you must explicitly pass undefined to the seedOrRNG parameter.

Type parameters

Name
T

Parameters

NameTypeDefault valueDescription
arrayreadonly T[]undefinedThe array to get an element from.
seedOrRNGundefined | RNG | SeedundefinedThe Seed or RNG object to use. If an RNG object is provided, the RNG.Next method will be called. If undefined is provided, it will default to a random seed.
exceptionsreadonly T[][]Optional. An array of elements to skip over if selected.

Returns

T

Defined in

packages/isaacscript-common/src/functions/array.ts:448


getRandomArrayElementAndRemove

getRandomArrayElementAndRemove<T>(array, seedOrRNG, exceptions?): T

Helper function to get a random element from the provided array. Once the random element is decided, it is then removed from the array (in-place).

If you want to get an unseeded element, you must explicitly pass undefined to the seedOrRNG parameter.

Type parameters

Name
T

Parameters

NameTypeDefault valueDescription
arrayT[]undefinedThe array to get an element from.
seedOrRNGundefined | RNG | SeedundefinedThe Seed or RNG object to use. If an RNG object is provided, the RNG.Next method will be called. If undefined is provided, it will default to a random seed.
exceptionsreadonly T[][]Optional. An array of elements to skip over if selected.

Returns

T

Defined in

packages/isaacscript-common/src/functions/array.ts:484


getRandomArrayIndex

getRandomArrayIndex<T>(array, seedOrRNG, exceptions?): int

Helper function to get a random index from the provided array.

If you want to get an unseeded index, you must explicitly pass undefined to the seedOrRNG parameter.

Type parameters

Name
T

Parameters

NameTypeDefault valueDescription
arrayreadonly T[]undefinedThe array to get the index from.
seedOrRNGundefined | RNG | SeedundefinedThe Seed or RNG object to use. If an RNG object is provided, the RNG.Next method will be called. If undefined is provided, it will default to a random seed.
exceptionsreadonly int[][]Optional. An array of indexes that will be skipped over when getting the random index. Default is an empty array.

Returns

int

Defined in

packages/isaacscript-common/src/functions/array.ts:512


includes

includes<T, TupleElement>(array, searchElement): searchElement is TupleElement

Similar to the Array.includes method, but works on a widened version of the array.

This is useful when the normal Array.includes produces a type error from an array that uses an as const assertion.

Type parameters

Name
T
TupleElement

Parameters

NameType
arrayreadonly TupleElement[]
searchElementWidenLiteral<T>

Returns

searchElement is TupleElement

Defined in

packages/isaacscript-common/src/functions/array.ts:532


isArray

isArray(object, ensureContiguousValues?): object is unknown[]

Since Lua uses tables for every non-primitive data structure, it is non-trivial to determine if a particular table is being used as an array. isArray returns true if:

  • the table contains all numerical indexes that are contiguous, starting at 1
  • the table has no keys (i.e. an "empty" table)

Parameters

NameTypeDefault valueDescription
objectunknownundefinedThe object to analyze.
ensureContiguousValuesbooleantrueOptional. Whether the Lua table has to have all contiguous keys in order to be considered an array. Default is true.

Returns

object is unknown[]

Defined in

packages/isaacscript-common/src/functions/array.ts:551


isArrayContiguous

isArrayContiguous(array): boolean

Helper function to see if every element in the array is N + 1.

For example, [2, 3, 4] would return true, and [2, 3, 5] would return false.

Parameters

NameType
arrayreadonly int[]

Returns

boolean

Defined in

packages/isaacscript-common/src/functions/array.ts:596


isArrayElementsUnique

isArrayElementsUnique(array): boolean

Helper function to check if all the elements of an array are unique within that array.

Under the hood, this is performed by converting the array to a set.

Parameters

NameType
arrayreadonly unknown[]

Returns

boolean

Defined in

packages/isaacscript-common/src/functions/array.ts:616


isArrayInArray

isArrayInArray<T>(arrayToMatch, parentArray): boolean

Checks if an array is in the provided 2-dimensional array.

Type parameters

Name
T

Parameters

NameType
arrayToMatchreadonly T[]
parentArrayreadonly readonly T[][]

Returns

boolean

Defined in

packages/isaacscript-common/src/functions/array.ts:622


setAllArrayElements

setAllArrayElements<T>(array, value): void

Helper function to set every element in an array to a specific value.

Type parameters

Name
T

Parameters

NameType
arrayT[]
valueT

Returns

void

Defined in

packages/isaacscript-common/src/functions/array.ts:631


shuffleArray

shuffleArray<T>(originalArray, seedOrRNG): T[]

Shallow copies and shuffles the array using the Fisher-Yates algorithm. Returns the copied array.

If you want an unseeded shuffle, you must explicitly pass undefined to the seedOrRNG parameter.

From: https://stackoverflow.com/questions/2450954/how-to-randomize-shuffle-a-javascript-array

Type parameters

Name
T

Parameters

NameTypeDescription
originalArrayreadonly T[]The array to shuffle.
seedOrRNGundefined | RNG | SeedThe Seed or RNG object to use. If an RNG object is provided, the RNG.Next method will be called. If undefined is provided, it will default to a random seed.

Returns

T[]

Defined in

packages/isaacscript-common/src/functions/array.ts:651


shuffleArrayInPlace

shuffleArrayInPlace<T>(array, seedOrRNG): void

Shuffles the provided array in-place using the Fisher-Yates algorithm.

If you want an unseeded shuffle, you must explicitly pass undefined to the seedOrRNG parameter.

From: https://stackoverflow.com/questions/2450954/how-to-randomize-shuffle-a-javascript-array

Type parameters

Name
T

Parameters

NameTypeDescription
arrayT[]The array to shuffle.
seedOrRNGundefined | RNG | SeedThe Seed or RNG object to use. If an RNG object is provided, the RNG.Next method will be called. If undefined is provided, it will default to a random seed.

Returns

void

Defined in

packages/isaacscript-common/src/functions/array.ts:674


sumArray

sumArray(array): number

Helper function to sum every value in an array together.

Parameters

NameType
arrayreadonly number[]

Returns

number

Defined in

packages/isaacscript-common/src/functions/array.ts:692


swapArrayElements

swapArrayElements<T>(array, i, j): void

Helper function to swap two different array elements. (The elements will be swapped in-place.)

Type parameters

Name
T

Parameters

NameType
arrayT[]
inumber
jnumber

Returns

void

Defined in

packages/isaacscript-common/src/functions/array.ts:700