Using typescript lets say i have an object set and I only want 10 of them. Currently, from my understanding, all I can do is .take(10)
. So if I want results 10-19? Then I can do a .take(20)
and manually trim. Which theoretically should work fine until I get to the end of the object set (or error on > 100k?)
So the feature request would be to have something like .offset(10)
to call on an object set.
To achieve what you’re describing in TypeScript, you can implement a such simple pagination method to fetch a subset of an array or object set, without needing to manually trim after a .take().
Here’s how you can approach this:
function paginate<T>(items: T[], offset: number, limit: number): T[] {
return items.slice(offset, offset + limit);
}
// Example usage:
const myArray = Array.from({ length: 100 }, (_, i) => i + 1); // [1, 2, ..., 100]
const result = paginate(myArray, 10, 10); // Fetch items from 10 to 19
console.log(result); // [11, 12, ..., 20]
- slice(offset, offset + limit): This is effectively what an .offset() f1unction would do.
- offset: The starting index of the subset.
- limit: The number of items to return.
Hope this helps in the meantime.
1 Like