dropRight()
dropRight<
T>(array,count):T[]
Creates a slice of array with n elements dropped from the end.
Type Parametersโ
T: Tโ
The type of elements in the array.
Parametersโ
array: readonly T[]โ
The array to query.
count: numberโ
The number of elements to drop.
Returns: T[]โ
A new array with the last count elements removed.
Throwsโ
RangeError If count is negative.
Sinceโ
2.0.0
Performanceโ
O(n) time & space, single slice operation.
Also known asโ
dropLast (Remeda, Ramda) ยท dropRight (Lodash, es-toolkit, Effect) ยท โ (Radashi, Modern Dash, Antfu)
Exampleโ
const numbers = [1, 2, 3, 4, 5];
dropRight(numbers, 2);
// => [1, 2, 3]
dropRight(numbers, 0);
// => [1, 2, 3, 4, 5]
How it works?โ
Drops elements from the end instead of the beginning.
Use Casesโ
Remove incomplete trailing data ๐โ
Exclude the last N entries that may be partial or unfinished. Perfect for real-time data streams or live metrics.
const liveMetrics = [
{ minute: "10:01", value: 150 },
{ minute: "10:02", value: 148 },
{ minute: "10:03", value: 152 },
{ minute: "10:04", value: 45 }, // Incomplete minute
];
const completeData = dropRight(liveMetrics, 1);
// => First 3 entries only
Exclude future-dated entriesโ
Remove scheduled or draft items from the end of sorted lists. Useful for publishing systems or event calendars.
const posts = [
{ title: "Published 1", date: "2025-01-10" },
{ title: "Published 2", date: "2025-01-15" },
{ title: "Scheduled", date: "2025-02-01" },
{ title: "Draft", date: "2025-03-01" },
];
const liveContent = dropRight(posts, 2);
// => Only published posts
Trim trailing placeholders from fixed-length recordsโ
Remove padding or filler values from data structures. Essential for parsing binary formats or legacy systems.
const record = ["John", "Doe", "john@mail.com", "", "", ""];
const cleanRecord = dropRight(record, 3);
// => ["John", "Doe", "john@mail.com"]