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"]