Array
import ArrayUtil from "@arclockproject/common/library/Array";
For examples and live demos, see the Array guide.
ArrayUtil
Object · default export
Helpers that change an array in place.
ArrayUtil.shuffle()
ArrayUtil.shuffle<k>(array: k[]): k[]
Shuffles array in place using the Fisher–Yates algorithm and Math.random().
Parameters
array(k[]) — Array to shuffle. It is changed.
Returns k[] — The same array, for chaining.
Example
const cards = [1, 2, 3, 4];
ArrayUtil.shuffle(cards); // e.g. [3, 1, 4, 2], and `cards` is now in that order too
ArrayUtil.swap()
ArrayUtil.swap<k>(array: k[], currentIndex: number, randomIndex: number): k[]
Swaps two elements of array in place.
Parameters
array(k[]) — Array to change.currentIndex(number) — Index of the first element.randomIndex(number) — Index of the second element.
Returns k[] — The same array, for chaining.
Example
ArrayUtil.swap(["a", "b", "c"], 0, 2); // ["c", "b", "a"]
ArrayUtil.remove()
ArrayUtil.remove<k>(array: k[], element: k): boolean
Removes the first element that is === to element, in place.
Parameters
array(k[]) — Array to change.element(k) — Element to find and remove.
Returns boolean — true if an element was removed, false if it was not found.
Example
const ids = [1, 2, 3, 2];
ArrayUtil.remove(ids, 2); // true, ids is now [1, 3, 2]