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