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