Skip to main content

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