dropWhile()
dropWhile<
T>(array,predicate):T[]
Creates a slice of array excluding elements dropped from the beginning. Elements are dropped until predicate returns falsey.
Type Parametersβ
T: Tβ
The type of elements in the array.
Parametersβ
array: readonly T[]β
The source array to query.
predicate: (value, index, array) => booleanβ
The function invoked per iteration.
Returns: T[]β
A new array with the leading elements dropped.
Sinceβ
2.0.0
Performanceβ
O(n) β single forward pass with final slice.
Also known asβ
dropWhile (Lodash, es-toolkit, Remeda, Ramda, Effect, Modern Dash) Β· β (Radashi, Antfu)
Exampleβ
dropWhile([1, 2, 3, 4, 5], (v) => v < 3);
// => [3, 4, 5]
dropWhile(
[{ name: 'Alice', active: false }, { name: 'Bob', active: true }],
(u) => !u.active
);
// => [{ name: 'Bob', active: true }]
How it works?β
Drops elements from the beginning while the predicate returns true. Stops at first falsy result.
Use Casesβ
Skip leading falsy or invalid entries πβ
Drop elements from the start of an array while they fail a validation check. Perfect for processing data streams, logs, or inputs that may have leading garbage values.
const logs = ["", "", "INFO: Started", "DEBUG: Loaded", "ERROR: Failed"];
const meaningfulLogs = dropWhile(logs, (log) => log === "");
// => ["INFO: Started", "DEBUG: Loaded", "ERROR: Failed"]
Skip sorted elements below a thresholdβ
Drop leading elements from a sorted array until a condition is met. Perfect for time-series data, paginated results, or filtering out outdated entries.
const transactions = [
{ date: new Date("2024-01-15"), amount: 100 },
{ date: new Date("2024-06-20"), amount: 250 },
{ date: new Date("2025-01-10"), amount: 75 },
{ date: new Date("2025-03-05"), amount: 300 },
];
const thisYearTransactions = dropWhile(
transactions,
(t) => t.date.getFullYear() < 2025
);
// => [{ date: 2025-01-10, amount: 75 }, { date: 2025-03-05, amount: 300 }]
Skip header rows in structured dataβ
Drop leading metadata or header rows before processing actual content. Perfect for parsing CSV files, API responses with metadata, or documents with preambles.
const csvRows = [
{ type: "header", value: "Name,Age,City" },
{ type: "header", value: "---,---,---" },
{ type: "data", value: "Alice,25,Paris" },
{ type: "data", value: "Bob,30,Lyon" },
];
const dataRows = dropWhile(csvRows, (row) => row.type === "header");
// => [{ type: "data", value: "Alice,25,Paris" }, { type: "data", value: "Bob,30,Lyon" }]