Skip to main content

Collections

All

collection.All -> boolean

Returns true if all elements in the collection are truthy, otherwise false. If the collection is empty, returns true. If given a value that is not an array or map, returns whether the value is truthy.

Any

collection.Any -> boolean

Returns true if any elements in the collection are truthy, otherwise false. If the collection is empty, returns false. If given a value that is not an array or map, returns whether the value is truthy.

Clear

collection.Clear -> collection

Removes all elements from an Array or a Map. The collection is modified in place, but is also returned for convenient chaining. If given a value that is not an Array or Map, returns the original value unchanged.

Filter

collection.Filter |value, key| { } -> array

Returns a new Array containing all elements of collection for which the predicate returns true.

The predicate is given the value and key of each element in the collection. For Arrays or Ranges, the key is the index of the element. For Maps, the key is the key of the element. The key is passed as the second parameter so you can easily ignore it, as most of the time you will not need it.

If collection is not an Array, Map or Range, returns undefined.

Example:

let numbers = [1, 2, 3, 4, 5];
let evens = numbers.Filter(|n| n % 2 == 0)

The evens array will contain [2, 4].

FindMax

collection.FindMax -> number

Returns the largest element in a collection, ignoring null and undefined values. If the collection is empty, returns undefined. If collection is not an Array or Map, returns the original value unchanged.

FindMin

collection.FindMin -> number

Returns the smallest element in the collection, ignoring null and undefined values. If the collection is empty, returns undefined. If collection is not an Array or Map, returns the original value unchanged.

Map

collection.Map |value, key| { } -> array

Returns a new Array containing the results of applying the selector function to each element of the collection.

The selector is given the value and key of each element in the collection. For Arrays or Ranges, the key is the index of the element. For Maps, the key is the key of the element. The key is passed as the second parameter so you can easily ignore it, as most of the time you will not need it.

If collection is not an Array, Map or Range, returns undefined.

Example:

let numbers = [1, 2, 3, 4, 5];
let squares = numbers.Map(|n| n * n)

The squares array will contain [1, 4, 9, 16, 25].

MapKeys

map.MapKeys -> keys

Returns a view of the keys of the given map. This can be used in most situations where an Array is expected, for example in a for loop. If given a value that is not a map, returns undefined.

MapValues

map.MapValues -> values

Returns a view of the values of the given map. This can be used in most situations where an Array is expected, for example in a for loop. If given a value that is not a map, returns undefined.

MaxBy

collection.MaxBy |element| { } -> value

Returns the maximum value in an array, using the result of the selector function. The selector is a function that takes an element of the collection and returns a value. The selector is called for each element of the collection.

If collection is nullish, returns undefined.

MinBy

collection.MinBy |element| { } -> value

Returns the minimum value in an array, using the result of the selector function. The selector is a function that takes an element of the collection and returns a value. The selector is called for each element of the collection. If selector returns undefined for any element, that element is skipped.

If collection is nullish, returns undefined.

OrderBy

collection.OrderBy |element| { } -> array

Sorts the elements of an array in ascending order, returning a new sorted array and leaving the original unchanged. The selector is a function that takes an element of the collection and returns a value to sort by. The selector is called for each element of the collection.

If collection is nullish, returns undefined.

OrderByDesc

collection.OrderByDesc |element| { } -> array

Sorts the elements of an array in descending order, returning a new sorted array and leaving the original unchanged. The selector is a function that takes an element of the collection and returns a value to sort by. The selector is called for each element of the collection.

If collection is nullish, returns undefined.

PickRandom

this?.PickRandom(collection) -> element

Returns a random element from the given collection.

  • this (Entity): Determine's which entity's random number generator to use. If nullish, uses World. This is optional and is only used to help increase stability if a network rollback occurs.
  • collection (Array, Map, or Range): The collection to pick from. If nullish, returns undefined.

Pop

collection.Pop -> element

Removes the last element from a collection and returns it. If the collection is empty, returns undefined.

If collection is an Array, the last element is removed and returned. If collection is a Map, the last key-value pair is removed and returned as an array. Otherwise, does nothing and returns undefined.

Push

collection.Push(value) -> collection

Adds an element to the end of a collection. The collection is modified in-place, but is also returned for convenient chaining.

  • If collection is an Array, value is added as a new element.
  • If collection is a Map, value is expected to be an Array of key-value pairs, and each pair is added to the map. Or if value is another map, all entries are added to the collection map.
  • Otherwise, returns the original collection unchanged.

Range

Range(from, to) -> range

Returns a range representing all integers between from and to (exclusive). If to is less than or equal to from, returns an empty range.

RangeInclusive

RangeInclusive(from, to) -> range

Returns a range representing all integers from from to to (inclusive). If to is less than from, returns an empty range.

Shuffle

this?.Shuffle(array) -> array

Shuffles an array in place. Returns the shuffled array.

  • this (Entity): Determine's which entity's random number generator to use. If nullish, uses World. This is optional and is only used to help increase stability if a network rollback occurs.
  • array (Array): The array to shuffle. If nullish, returns undefined.

Sort

collection.Sort -> collection

Sorts the elements of an array in ascending order. The array is modified in place. It is returned to allow for chaining. If the collection is not an array, it is returned unchanged.

SortDesc

SortDesc(collection) -> collection

Sorts the elements of an array in descending order. The array is modified in place. It is returned to allow for chaining. If the collection is not an array, it is returned unchanged.

Sum

collection.Sum -> number

Returns the sum of all numbers in the collection. All non-number elements are ignored. If the collection is empty, returns 0. If collection is not a collection, returns the original value if it is a number, otherwise returns 0.

VectorSum

collection.VectorSum -> vector

Returns the sum of all Vector elements in the collection. All non-Vector elements are ignored. If the collection is empty, returns @(0,0). If collection is not a collection, returns the original value if it is a Vector, otherwise returns @(0,0).