Arrays
Utility functions for working with arrays.
Quick Array
Generates an array of a specified length. If a value is provided, the array will be filled with that value. Otherwise, it will contain consecutive numbers starting from 1.
export function quickArray(length: number, fillValue?: any) {
return Array.from({ length }, (_, i) => fillValue || i + 1);
}Get Element and Index from Array
Searches through an array and returns the first element that satisfies the given predicate, along with its index. If no match is found, it returns [null, -1].
export function findElement<T>(
array: T[],
predicate: (item: T) => boolean
): [T, number] | [null, -1] {
for (let i = 0; i < array.length; i++) {
if (predicate(array[i])) {
return [array[i], i];
}
}
return [null, -1];
};