Limit Array

Limit an array to a certain number of elements and return the remaining count.

CategoryArrays
AuthorPol Gubau
Since1.0.0
Last Updated2025-03-15
TypeSync
CompatibilityUniversal

Example

  • Input

    limitArray([1, 2, 3, 4, 5], 3)

    Output:

    { limitedArray: [1, 2, 3], remaining: 2 }

Return

An object containing the limited array and the count of remaining elements.

Type: { limitedArray: T[], remaining: number }

Props

NameTypeDefaultRequiredDescription
arrT[]nullYesThe array to be limited.
limitnumber3NoThe maximum number of elements to keep.

Notes

If the array length is less than or equal to the limit, `remaining` will be 0.

Tags

  • array
  • limit
  • slice

Related